1

I'm trying to create a barebones list maker for what once was a school project and is now a personal project (missed deadline), and I ran into a wall pretty much instantly.

When trying to do

def create(list):
   lists.append(str(list))
   str(list) = []

I get the error

SyntaxError: cannot assign to function call

Is this impossible? The idea is to have a user type commands such as "create (name of list)" and "add (name of list) (name of item)" and I think I may have to completely change my plan.

*also lists.append() refers to a list of the list names that is NOT a 2D list, though I'm considering using one

3
  • Your code is not complete. Where the lists were defined? Commented May 8, 2021 at 3:37
  • This is an example. Defining lists is not necessary right now. Commented May 8, 2021 at 4:34
  • 3
    Do not name an argument one of the default Python types, in this case "list". This is likely happening because of one of the PIP updates allowing you to specify the type of input argument types for sanity checking. Try renaming "list" to something else Commented May 8, 2021 at 5:38

2 Answers 2

2

I suggest using a dictionary instead.
You can create a new key/value pair when calling create.

Also, please do not shadow in-built functions. This means that names like list or str as variable names or args should not be used.

my_dict = {} # Put this somewhere at the top of your program so all methods can access
def create(l):
    lists.append(str(l))
    my_dict[str(l)] = []
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is not complete. Where the lists were defined?
@MauroBaraldi Idk, it's defined by OP. Not me
1

The error you've got comes from this line of code:

str(list) = []

On the left side in an assignment, you have to put a name of a variable to which you want to assign. str(list) is no variable name and hence, it cannot work this way. Except of that, as already suggested by @12944qwerty and @Parad0x13, you shouldn't use list as a variable name to prevent conflicts (if you assign to list, you are redefining meaning of list and risk to be unable to use the function list subsequently - directly or indirectly via other functions).

If I understand your question well, you want to let the user to specify a name of a list to be created, create an empty list with that name and add that list name to a list of existing lists. More practical solutions were shown, e.g., in How do I create variable variables? and discussed broadly in How can you dynamically create variables via a while loop?. There are good solutions involving dictionaries; if you want to use other approaches (e.g., using exec, globals or locals), please read the linked discussions to understand related risks and problems.

1 Comment

Thank you, I'm pretty new to coding in general so I'll probably be referring to those later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.