map/filter/reduce/lambda opinions and background unscientific mini-survey

Steven Bethard steven.bethard at gmail.com
Sun Jul 3 19:28:14 EDT 2005


Christopher Subich wrote:
> One caevat that I just noticed, though -- with the for-solution, you do 
> need to be careful about whether you're using a generator or list if you 
> do not set an explicit initial value (and instead use the first value of 
> 'sequence' as the start).  The difference is:
> _accum = g.next()
> for i in g: _accum = stuff(_accum,i)
> 
> versus
> _accum = g[0]
> for i in g[1:]: _accum = stuff(_accum,i)

If you want to be general for all iterables (list, generators, etc), you 
can write the code like:

itr = iter(g)
_accum = itr.next()
for i in itr:
     _accum = stuff(_accum, i)

STeVe



More information about the Python-list mailing list