Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python create a list with a variable in the name [duplicate]

I'm trying to generate a series of (empty) lists using a for loop in python, and I want the name of the list to include a variable. e.g. y0, y1, y2 etc. Ideally looking something like this:

for a in range (0,16777216):
global y(a)=[]
like image 829
schwal Avatar asked Feb 03 '26 22:02

schwal


2 Answers

why wouldn't you do a dictionary of lists?

y = {}
for a in range (0,16777216):
    y[a] = []

also for brevity: https://stackoverflow.com/a/1747827/884453

y = {a : [] for a in range(0,16777216)}
like image 75
Francis Yaconiello Avatar answered Feb 06 '26 10:02

Francis Yaconiello


Not sure if this is quite what you want but couldn't you emulate the same behavior by simply creating a list of lists? So your_list[0] would correspond to y0.

like image 25
Tejas Sharma Avatar answered Feb 06 '26 10:02

Tejas Sharma