By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

How do I not make a list?

P: n/a
It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
I hope I've described this adequately.
Thank you...

Nov 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Just Another Victim of the Ambient Morality schrieb:
It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
You want a generator expression. Or a generator.
res = (apply_something(e) for e in my_iterable)
or

def g(mit):
for e in mit:
yield apply_something(e)
Both only get evaluated step by step during the iteration, reducing
memory consumption.

Diez
Nov 29 '07 #2

P: n/a
On 11/29/07, Just Another Victim of the Ambient Morality
<ih*******@hotmail.comwrote:
It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
Well I think what you want is to use "()" instead of "[]"
>>l = (i for i in range(1,20))
l
<generator object at 0xb7f482ac>

Cheers,
--
Amit Khemka
Nov 29 '07 #3

P: n/a
On 2007-11-29, Just Another Victim of the Ambient Morality
<ih*******@hotmail.comwrote:
It may sound like a strange question but that's probably only
because I don't know the proper terminology. I have an
iterable object, like a list, and I want to perform a transform
on it (do an operation on each of the elements) and then pass
it onto something else that expects and iterable. I'm pretty
sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Try itertools.imap.

something_else(imap(do_operation, an_iterable))

--
Neil Cerutti
You've got to take the sour with the bitter. --Samuel Goldwyn
Nov 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.