From: kj on



....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
From: Jon Clements on
On 13 Nov, 21:26, kj <no.em...(a)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.
From: kj on
In <6e20a31b-2218-49c5-a32c-5f0147db3172(a)k19g2000yqc.googlegroups.com> Jon Clements <joncle(a)googlemail.com> 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
From: Diez B. Roggisch on
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
From: Paul Rubin on
kj <no.email(a)please.post> writes:
> lol = [None] * 500
> for i in xrange(len(lol)):
> lol[i] = []

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