creating really big lists

Paul Rudin paul.nospam at rudin.co.uk
Wed Sep 5 06:35:05 EDT 2007


"Diez B. Roggisch" <deets at nospam.web.de> writes:

> Paul Rudin wrote:
>
>> Dr Mephesto <dnhkng at googlemail.com> writes:
>> 
>>> Hi!
>>>
>>> I would like to create a pretty big list of lists; a list 3,000,000
>>> long, each entry containing 5 empty lists. My application will append
>>> data each of the 5 sublists, so they will be of varying lengths (so no
>>> arrays!).
>>>
>>> Does anyone know the most efficient way to do this? I have tried:
>>>
>>> list = [[[],[],[],[],[]] for _ in xrange(3000000)]
>>>
>>> but its not soooo fast. Is there a way to do this without looping?
>> 
>> You can do:
>> 
>>    [[[],[],[],[],[]]] * 3000000
>> 
>> although I don't know if it performs any better than what you already
>> have.
>
> You are aware that this is hugely different, because the nested lists are
> references, not new instances? Thus the outcome is most probably (given the
> gazillion of times people stumbled over this) not the desired one...

Err, yes sorry. I should try to avoid posting before having coffee in
the mornings.



More information about the Python-list mailing list