0

I'm trying to make 1 list for every item in this list, but the length of the list is based on user input, so that is why I'm using a for a loop.

My Idea:

listno = 0
for i in arialabel:
    a+str(listno) = []
    print(a+str(listno))
    listno+=1

My current problem is that you can't use operators in the names of variables.

2
  • yourlists = []; for _ in range(howmany): yourlists.append([]) Commented Sep 18, 2020 at 17:19
  • Yes you should not use that, please provide more info of what you are doing.I would suggest you to simply make a list of list for that usage. Commented Sep 18, 2020 at 17:39

1 Answer 1

1

Put your lists in a dictionary:

arialabel = ['w', 'x', 'y', 'z']
prefix = "a"

lists = {}
listno = 0
for i in arialabel:
    lists[prefix+str(listno)] = []
    print(prefix+str(listno))
    listno+=1

print(lists)

Result:

a0
a1
a2
a3
{'a0': [], 'a1': [], 'a2': [], 'a3': []}

Example of usage:

lists['a3'].append("apple")
lists['a3'].append("pear")
print(lists['a3'])

Example result:

['apple', 'pear']
Sign up to request clarification or add additional context in comments.

2 Comments

I think a+str(listno) was a variable OP was trying to define with that name, i.e. a1, a2, ... So in order to replicate what they want, you'd replace a+str(listno) with "a"+str(listno). Also you can demonstrate how to get the value from the dict which might be helpful.
ah, ok. easy. just change prefix

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.