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

bluemonsta
  • 11
  • 2
  • Your code is not complete. Where the `lists` were defined? – Mauro Baraldi May 08 '21 at 03:37
  • This is an example. Defining lists is not necessary right now. – Buddy Bob May 08 '21 at 04: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 – Parad0x13 May 08 '21 at 05:38

2 Answers2

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)] = []
12944qwerty
  • 2,001
  • 1
  • 10
  • 30
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.