different ways to creating two lists from one
Diez B. Roggisch
deets_noospaam at web.de
Wed Oct 29 16:03:42 EST 2003
> But suppose generating s0 is expensive and/or s0 is big. In otherwords I'd
> like to go over only once and I don't want to keep it around any longer
> than I have to.
>
> I could do something like:
>
> for a, b in [(x + x, x * x) for x in s0]:
> s1.append(a)
> s2.append(b)
>
> Is there a better / cleaner way that I'm missing?
Why the uneccessary list comprehension? That makes only sense if you want to
actually keep it. Which you say you don't want :)
So simply
for x in s0:
s1.append(2*x)
s2.append(x*x)
should do the trick. Especially if s0 itselft is an iterator.
Diez
More information about the Python-list
mailing list