creating really big lists

Bryan Olson fakeaddress at nowhere.org
Wed Sep 5 12:33:24 CEST 2007


Paul Rudin wrote:
> Dr writes:
>> 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.

Actually, that produces list of 3000000 references to the same
5-element list. A reduced example:

 >>> lst = [[[],[],[],[],[]]] * 3
 >>> lst[1][1].append(42)
 >>> print lst
[[[], [42], [], [], []], [[], [42], [], [], []], [[], [42], [], [], []]]


-- 
--Bryan



More information about the Python-list mailing list