list of empty lists

Carl Banks idot at vt.edu
Tue Mar 26 23:12:37 EST 2002


Curtis Jensen <cjensen at bioeng.ucsd.edu>,
exactly what drugs were you under the influence of when you 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.

Python >= 2.0:

    l = [[] for i in xrange(0,10)]

Any Python:

    l = map(lambda x:[],range(0,10))

    from copy import copy
    l = map(copy,[[]]*10)


-- 
CARL BANKS

"I like nothing better than a good challenge.
 This is not one of them."



More information about the Python-list mailing list