list of empty lists

Robin Becker robin at jessikat.fsnet.co.uk
Wed Mar 27 04:19:19 EST 2002


In article <3CA11A5B.5000501 at bioeng.ucsd.edu>, Curtis Jensen
<cjensen at bioeng.ucsd.edu> writes
>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.
>
there was a thread about this a while back mainly focussed on the most
efficient way to do this for big lists. I think the winner was something
like

map(list, [[]]*n)


-- 
Robin Becker



More information about the Python-list mailing list