Is it possible to convert a list-type into a generator without iterating through?

Phillip B Oldham picture Phillip B Oldham · May 9, 2011 · Viewed 40.7k times · Source

I know that it's possible to convert generators into lists at a "low-level" (eg. list(i for i in xrange(10))), but is it possible to do the reverse without iterating through the list first (eg. (i for i in range(10)))?

Edit: removed the word cast for clarity in what I'm trying to achieve.

Edit 2: Actually, I think I may have misunderstood generators at a fundamental level. That'll teach me to not post SO questions before my morning coffee!

Answer

John Machin picture John Machin · May 9, 2011

Try this: an_iterator = iter(a_list) ... docs here. Is that what you want?