Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > The ol' [[]] * 500 bug...

Reply
Thread Tools

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

 
 
kj
Guest
Posts: n/a
 
      11-13-2009



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

Yikes. 10 miles uphill, both ways...

kynn
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-13-2009
On 13 Nov, 21:26, kj <no.em...@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.
 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      11-13-2009
In <6e20a31b-2218-49c5-a32c-> 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
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-14-2009
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      11-14-2009
kj <> writes:
> lol = [None] * 500
> for i in xrange(len(lol)):
> lol[i] = []


lol = map(list, [()] * 500)
 
Reply With Quote
 
Vlastimil Brom
Guest
Posts: n/a
 
      11-14-2009
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[i] = []

>>
>> 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
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      11-14-2009
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

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      11-14-2009
Ulrich Eckhardt <> writes:
> That said, [[]]*500 is IMHO more readable.


But the issue in the thread is that it does the wrong thing.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-15-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SinTek 500 SLI 500W Power Supply Silverstrand Front Page News 0 10-05-2005 02:01 PM
Antec Phantom 500 Power Supply Review at XYZ Computing Silverstrand Front Page News 0 08-23-2005 01:29 AM
Hauppauge PVR 500 MCE Silverstrand Front Page News 6 07-04-2005 05:07 PM
PIX log reporting 169.254.126.114/500 dst outside:192.168.100.2/500 hoser Cisco 2 04-15-2005 05:22 PM
stack overflow just because of a double array[500][500]? James C++ 2 11-03-2004 09:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57