How do I not make a list?

Amit Khemka khemkaamit at gmail.com
Thu Nov 29 03:39:04 EST 2007


On 11/29/07, Just Another Victim of the Ambient Morality
<ihatespam at hotmail.com> wrote:
>     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



More information about the Python-list mailing list