10

I'm trying to create multiple lists like the following:

l1 = []  
l2 = []  
..  
ln = []  

Is there any way to do that?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Sathish Bowatta
  • 151
  • 1
  • 1
  • 8
  • 12
    Why not use a dictionary instead, or use a nested list? Whenever you are thinking: *I need to generate variable names*, **stop**. You want a dictionary instead, with your 'variable names' keys in that dictionary. If the names are sequential (numbered starting at 0 or 1), use a list instead. – Martijn Pieters Jun 02 '14 at 17:08
  • It is always better to tell what do you really want to implement -- I mean the *bigger picture*. The solution may be different than you think. – pepr Jun 25 '14 at 18:56
  • 2
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – wjandrea Aug 22 '21 at 18:00

6 Answers6

14

What you can do is use a dictionary:

>>> obj = {}
>>> for i in range(1, 21):
...     obj['l'+str(i)] = []
... 
>>> obj
{'l18': [], 'l19': [], 'l20': [], 'l14': [], 'l15': [], 'l16': [], 'l17': [], 'l10': [], 'l11': [], 'l12': [], 'l13': [], 'l6': [], 'l7': [], 'l4': [], 'l5': [], 'l2': [], 'l3': [], 'l1': [], 'l8': [], 'l9': []}
>>> 

You can also create a list of lists using list comprehension:

>>> obj = [[] for i in range(20)]
>>> obj
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • 1
    You could do `[[]]*20` instead of using the `range` function. – BeetDemGuise Jun 02 '14 at 17:43
  • 3
    `[[]] * 20` gives you a list of 20 references for the inner `[ ]` instead, this is incorrect. Try appending 1 value into the list, all of them will have that values too. Indeed if `L = [[]]*20` then `L[0] is L[1]` will be `True` – lowzhao Apr 29 '20 at 17:22
3

You can use dictionary comprehension:

obj = {i:[] for i in list(range(1,5))}
Cindyleee
  • 31
  • 2
1

Create a list of lists:

lists = []
n = 20
for i in range(n):
    lists.append([])

print lists[0] # Prints []
print lists[19] # Prints []
huu
  • 7,032
  • 2
  • 34
  • 49
1

Just Extending Already Accepted Answer, Using Enumerate to initiate default values in list. Accessing list and adding values which was not mentioned.

range_list = list(range (int(input())))
obj = {}


for i, j in enumerate(range_list): # assigning default values
    obj['l'+str(i)] = [j**2]
   
print(obj['l0'])
print(type(obj['l0']))

obj['l0'] = list(range(5)) # accessing list
print(obj['l0'])

#3 asking user, how many list to be created 
#[0] default value in first list
#<class 'list'>
#[0, 1, 2, 3, 4] current value in first list

#[Program finished]
Subham
  • 397
  • 1
  • 6
  • 14
  • Could you provide more details about your answer. Please have a look at https://stackoverflow.com/help/how-to-answer – Rishabh Kumar Feb 24 '21 at 07:11
  • what part were you not able to understand? I am just extending already accepted answer and have made lots of comments. – Subham Feb 24 '21 at 07:13
  • Try adding a line towards how your answer is different. See other posts in this thread to get an idea of what I am talking about. – Rishabh Kumar Feb 24 '21 at 07:17
0

You can simply use a for loop to create n lists.

for i in range(10): a_i = [i] #Stores the corresponding value of i in each a_i list. print(a_i)

The variable a_i changes as i increments, so it becomes a_1, a_2, a_3 ... and so on.

Akhzar Farhan
  • 7
  • 1
  • 1
  • 6
0

there is a way to do that if you want to create variables. but instead, u should use a dictionary as mentioned above. but anyway I am going to show you how to create variables as you asked.

for i in range(num_vars):
     globals()[f'ls{i}'] = []

print(ls1) # lsn should work
Hyperx837
  • 773
  • 5
  • 13