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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 14 23:19:55 EST 2009


On Fri, 13 Nov 2009 21:26:01 +0000, kj wrote:

> ...just bit me in the "fuzzy posterior".  

It's not a bug. Just because it doesn't behave as you would like it to 
behave doesn't mean it isn't behaving as designed.


> The best I can come up with is the hideous
>
>   lol = [[] for _ in xrange(500)]

That's not hideous.

 
> Is there something better?  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...

What's wrong with that?


lol = []
for _ in xrange(500): lol.append([])


is a simple alternative too, although the list comp is better.


-- 
Steven



More information about the Python-list mailing list