-2

I have the following lists

list_0=[]
list_1=[]
list_2=[]

I want to assign to each list a value in a for loop. so that in the first iteration, I can assign a value to the list_0, by the second iteration , a value to the list_1 and so forth. how can I do it ?

Thanks

ABA
  • 37
  • 5
  • 2
    have a list containing your lists `bigList = [list_0, list_1, list_2]`. so in your loop you can iterate `i` and access each sublist as `bigList[i]`. You can then assign a value to the list_i doing `bigList[i] = [1,2,3]` –  Apr 05 '22 at 13:39
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – 0x5453 Apr 05 '22 at 13:41
  • @SembeiNorimaki better example than `bigList[i] = "foo"` might be `bigList[i].append("foo")` – jarmod Apr 05 '22 at 13:42

1 Answers1

1

As highlighted in the comments you can make a biglist of your list e.g.

bigList = [list_0, list_1, list_2]
for i in range(3):
    my_list = biglist[i] # list according to i is fetched

Another approach i suggest to you is to make a dictionary

myList = {0: list_0, 1: list_1, 2: list_2}
for i in range(3):
    my_list = biglist[i] # list according to i is fetched
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29