list of empty lists

xtian xtian at toysinabag.com
Wed Mar 27 01:14:05 EST 2002


Curtis Jensen <cjensen at bioeng.ucsd.edu> wrote in message news:<3CA11A5B.5000501 at bioeng.ucsd.edu>...
> Is there a easy way to make a list of an arbitray number of empty lists?
> 
> In this case, what I would like is three independantly empty lists.  I 
> can make a for loop to loop "some_number" of times and append an empty 
> list at each itteration.  Is there a simpler way?
> Thanks.

Another way to avoid the shared inner list would be

>>> l = [[] for i in range(3)]
>>> l
[[], [], []]
>>> l[0].append(1)
>>> l
[[1], [], []]

The empty list is evaluated for each element in the source list.

(This is effectively the same as the for loop option, in that they
both introduce a counter variable.)

HTH
xtian

how nuch do I love
ninjas? let me count the ways:
1, 2, 3, 4, 5



More information about the Python-list mailing list