The ol' [[]] * 500 bug...

Jon Clements joncle at googlemail.com
Fri Nov 13 22:56:56 CET 2009


On 13 Nov, 21:26, kj <no.em... at please.post> wrote:
> ...just bit me in the "fuzzy posterior".  The best I can come up with
> is the hideous
>
>   lol = [[] for _ in xrange(500)]
>
> Is there something better?  

That's generally the accepted way of creating a LOL.

> What did one do before comprehensions
> were available?  I suppose in that case one would have to go all
> the way with
>
>   lol = [None] * 500
>   for i in xrange(len(lol)):
>       lol[i] = []
>
> Yikes.  10 miles uphill, both ways...
>

>>> lol = map(lambda L: [], xrange(5))
>>> [id(i) for i in lol]
[167614956, 167605004, 167738060, 167737996, 167613036]

Jon.



More information about the Python-list mailing list