0

I am trying to create n number of lists like

a_1 = [some data]
a_2 = [some data]
a_3 = [some data]
.          .
.          .
.          .
.          .
.          .
a_n = [some data]

I thought for looping would help

for i in range(10):
    a_i = []
    print(a_i)

My approach is fully wrong because the output I was expecting should be like

a_1 = [some data]
a_2 = [some data]
a_3 = [some data]
.          .
.          .
.          .
.          .
.          .
a_n = [some data]

instead of

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

Any help would be highly appreciated.

Ebad
  • 51
  • 6
  • Where is your data stored or how you are getting it? Please mentioned that as well. – Jayesh Dhandha Sep 24 '19 at 07:34
  • ignore the data part just help me with n number of empty list , however the list should be initialized like the way I have mentioned @JayeshDhandha – Ebad Sep 24 '19 at 07:36
  • If you will not apply any data than what approach you have done is fine! It will be empty list. – Jayesh Dhandha Sep 24 '19 at 07:39
  • but the whole output is now under a_i, the output I want is a_1 =[] , a_2 =[], ....a_n = [] @JayeshDhandha – Ebad Sep 24 '19 at 07:41
  • Why don't you use list of lists? – Jayesh Dhandha Sep 24 '19 at 07:44
  • that approach won't help me, the problem I am solving is I am getting n number of different inputs and I have to save it in n number of different lists with different initialization @JayeshDhandha – Ebad Sep 24 '19 at 07:51
  • yes almost duplicate but I got the idea thank you @JayeshDhandha – Ebad Sep 24 '19 at 07:54
  • Please paste your answer so that others can get benefit of it. If it's different than the one which told. Thanks! – Jayesh Dhandha Sep 24 '19 at 09:34
  • With your approach, in each loop `a_i` is overwritten each time. It does not take the values a_1, a_2 etc, it just reinitialize the list a_i over and over. – liakoyras Sep 24 '19 at 13:14

2 Answers2

0

You could also:

for i in range(5):
    exec('a_%d = []'%i)

Don't know if it has (dis)advantages over the globals method suggested in one of the comments.

Demi-Lune
  • 1,868
  • 2
  • 15
  • 26
0

Could try:

list_of_lists = []
for i in range(10):
    a_i = [] # put some data on the list
    list_of_lists.append(a_i)
print(lists_of_lists)
Rob
  • 415
  • 4
  • 12