list of empty lists

Curtis Jensen cjensen at bioeng.ucsd.edu
Wed Mar 27 10:46:28 EST 2002


Curtis Jensen wrote:
> Is there a easy way to make a list of an arbitray number of empty lists?
> 
> I can do something like:
>  >>> some_number = 3
>  >>> l = [[]] * some_number
>  >>> l
> [[], [], []]
> 
> Unfortunatly this has an unfortunate side affect.  Whatever I do to one 
> of the list gets done to the other lists. ie:
>  >>> l[0].append(1)
>  >>> l
> [[1], [1], [1]]
> 
> 
> 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.
> 

Thanks to all for the quick answers.
The "map(list, [[]]*n)" solution works best for me.  Sorry I didn't 
mention it earlier; we needed a Python 1.5.2 solution.  Plus this 
approach works on versions >1.5.

-- 
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451




More information about the Python-list mailing list