0

I would like to generate lists whose names include the iterator. Something like:

 for i in range(3):
     list_{here is the number}=[i,i+2,i+10]

The desired output:

>>list_0
[0,2,10]
>>list_1
[1,3,11]
>>list_2
[2,4,12]

Thanks for any tips

MirrG
  • 406
  • 3
  • 10

2 Answers2

1

Look into dictionary comprehension:

result = {f'list_{i}': [i, i + 2, i + 10] for i in range(3)}
  • Thank you for the answer. I was thinking about dictionaries, but is there a way to do it with the lists? I need lists as I work further with the indexes, which makes it cumbersome in case of dictionaries. – MirrG Nov 15 '19 at 16:34
  • What do you mean by lists? The other answer generates a list. You can iterate over a dictionary as well. This generates a dictionary where each key points to a list. – Error - Syntactical Remorse Nov 15 '19 at 16:35
  • I mean you propose to generate a dictionary with the desired names as keys, while I am trying to generate, if possible, just the three single lists – MirrG Nov 15 '19 at 16:38
  • Thats exactly what the answer below does. – Error - Syntactical Remorse Nov 15 '19 at 17:10
0

Is this what you want?

[[i,i+2,i+10] for i in range(3)]
seralouk
  • 30,938
  • 9
  • 118
  • 133
  • I did not downvote; but I think it is because, OP wants variables (with names) to be like `list_{index}`, but your answer just provides a single nested list. – mshsayem Nov 15 '19 at 15:52
  • @mshsayem It must have been more than that because who ever downvoted did it to both answers. I assume it was one of those, its a duplicate so don't answer downvotes. – Error - Syntactical Remorse Nov 15 '19 at 15:53