different ways to creating two lists from one

T

Todd MacCulloch

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?
 
D

Diez B. Roggisch

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
 
A

Alex Martelli

Todd said:
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?

Try timing your solutions, with an s0 as long as you want: I would be
very surprised if the first approach wasn't faster than the second one.

Assuming it's impossible to loop repeatedly on s0 AND the thing is too
huge to store anywhere, you can turn a sequence of pairs into a pair
of sequences with zip(*seqofpairs), but, again, you should measure
the speed rather than assuming the "cool" solution is fast:).


Alex
 
F

Fredrik Lundh

Todd said:
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)
ouch.

Is there a better / cleaner way that I'm missing?

for x in s0:
s1.append(x + x)
s2.append(x * x)
del s0

(memorywise, that's no better than your first alternative.
it's a better than your second alternative, though...)

if s0 contains large and ugly things, you could do something
like this:

for i, x in enumerate(s0):
s1.append(x + x)
s2.append(x * x)
s0 = None

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top