different ways to creating two lists from one

Todd MacCulloch wtodd.macculloch at sympatico.ca
Wed Oct 29 15:39:17 EST 2003


Suppose I have a master list and I need to create two derived lists,
something like:

s0 = [2,3,4]
s1 = [x + x for x in s0]
s2 = [x * x for x in s0]

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?







More information about the Python-list mailing list