"Collapsing" a list into a list of changes

Nick Coghlan ncoghlan at iinet.net.au
Fri Feb 4 23:31:08 EST 2005


Jack Diederich wrote:
> Since this is 2.4 you could also return a generator expression.
> 
> 
>>>>def iter_collapse(myList):
> 
> ...   return (x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]))
> ... 

But why write a function that returns a generator expression, when you could 
just turn the function itself into a generator?

Py>def iter_collapse(myList):
...   for x in it.groupby(myList):
...     yield x[0]

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list