How do I not make a list?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

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

Diez B. Roggisch

Just said:
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
 
A

Amit Khemka

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 "[]"
<generator object at 0xb7f482ac>

Cheers,
 
N

Neil Cerutti

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))
 

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

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top