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

Discussion in 'Python' started by kj, Nov 13, 2009.

1. kjGuest

....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? 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 = []

Yikes. 10 miles uphill, both ways...

kynn

kj, Nov 13, 2009

2. Jon ClementsGuest

On 13 Nov, 21:26, kj <> 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 = []
>
> Yikes.  10 miles uphill, both ways...
>

>>> lol = map(lambda L: [], xrange(5))
>>> [id(i) for i in lol]

[167614956, 167605004, 167738060, 167737996, 167613036]

Jon.

Jon Clements, Nov 13, 2009

3. kjGuest

In <> Jon Clements <> writes:

>>>> lol =3D map(lambda L: [], xrange(5))
>>>> [id(i) for i in lol]

>[167614956, 167605004, 167738060, 167737996, 167613036]

Oh yeah, map! I'd forgotten all about it. Thanks!

kynn

kj, Nov 13, 2009
4. Diez B. RoggischGuest

kj schrieb:
> ...just bit me in the "fuzzy posterior". The best I can come up with
> is the hideous
>
> lol = [[] for _ in xrange(500)]

If you call that hideous, I suggest you perform the same exercise in
Java or C++ - and then come back to python and relax....

Diez

Diez B. Roggisch, Nov 14, 2009
5. Paul RubinGuest

kj <> writes:
> lol = [None] * 500
> for i in xrange(len(lol)):
> lol = []

lol = map(list, [()] * 500)

Paul Rubin, Nov 14, 2009
6. Vlastimil BromGuest

2009/11/14 Brian J Mingus <>:
>
>
> On Sat, Nov 14, 2009 at 1:50 AM, Paul Rubin <http://>
> wrote:
>>
>> kj <> writes:
>> >   lol = [None] * 500
>> >   for i in xrange(len(lol)):
>> >       lol = []

>>
>> lol = map(list, [()] * 500)

>
> Could someone explain what the deal is with this thread? Thanks.
> [[]]*500
>

Try
>>> lst=[[]]*500
>>> lst[7].append(2)
>>> lst

to see...

vbr

Vlastimil Brom, Nov 14, 2009
7. Ulrich EckhardtGuest

Diez B. Roggisch wrote:
> kj schrieb:
>> lol = [[] for _ in xrange(500)]

>
> If you call that hideous, I suggest you perform the same exercise in
> Java or C++ - and then come back to python and relax....

I might be missing something that's not explicitly mentioned here, but I'd
say that all non-associative containers in C++ have a resize() method, some
even taking an initial size in their constructor.

That said, [[]]*500 is IMHO more readable.

Uli

Ulrich Eckhardt, Nov 14, 2009
8. Paul RubinGuest

Ulrich Eckhardt <> writes:
> That said, [[]]*500 is IMHO more readable.

But the issue in the thread is that it does the wrong thing.

Paul Rubin, Nov 14, 2009
9. Steven D'ApranoGuest

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 = []
>
> 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

Steven D'Aprano, Nov 15, 2009