473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

different ways to creating two lists from one

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?

Jul 18 '05 #1
3 1586
> 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
Jul 18 '05 #2
Todd MacCulloch wrote:
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

Jul 18 '05 #3
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>


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard Thornley | last post by:
Hello, I need some clarification in creating objects. Consider the following code... (note: The function InitializeListCombo initializes the combobox) Private daLists As New...
6
by: Doug Mazzacua | last post by:
I noticed that that <ul> lists display differently with <br> between <li>s in IE and Mozilla. Does anyone know of a reference that catalogs all of the known types of differences in display...
6
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious......
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
3
by: Jukka K. Korpela | last post by:
Some time ago in this group, someone suggested that we should develop a "different" user style sheet to demonstrate what a user style sheet or a browser style sheet _could_ do. I guess the idea was...
9
by: alexandis | last post by:
I have a big database, a lot of tables, so I will have a lot of pages where i create a new record. There will be a lot of 'reference' items - let's say 'Create user' -> 'Select user type'...
6
by: =?Utf-8?B?d2lsbGlhbQ==?= | last post by:
Hi, I posted this in wrong group, so just re-post here. There are two ways to create web-based application or web service, from VS start page, click on File and New, two options you can choose,...
11
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
2
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.