How does one generate a set of lists that incorporate the index from a for loop in the list name:
for j in range(10):
li"j" = []
How can the index 'j' be part of the name, so the lists are li0, li1, li2, ...
Thanks!
How does one generate a set of lists that incorporate the index from a for loop in the list name:
for j in range(10):
li"j" = []
How can the index 'j' be part of the name, so the lists are li0, li1, li2, ...
Thanks!
You can make li
a dictionary:
li = {}
for j in range(10):
li[j] = []
You can actually do this with exec, like so:
exec ("li%s = []" % 4)
But don't do this. You almost certainly do not want dynamically named variables. Greg Hewgill's approach will probably solve your problem.
If you do this simply to initialize lists to use them later, you should instead use one multi-dimensional list or even better tuple to do this:
li = tuple( [] for i in range( 10 ) )
li[0].append( 'foo' )
li[5].append( 'bar' )