different ways to creating two lists from one
Fredrik Lundh
fredrik at pythonware.com
Wed Oct 29 16:16:30 EST 2003
Todd MacCulloch wrote:
> 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[i] = None
</F>
More information about the Python-list
mailing list