How do I not make a list?
Diez B. Roggisch
deets at nospam.web.de
Thu Nov 29 03:36:50 EST 2007
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
More information about the Python-list
mailing list