[Tutor] Working with lists

bob gailer bgailer at gmail.com
Sun Dec 14 17:16:08 CET 2008


Paul McGuire wrote:
> Even simpler than Rich Lovely's:
>
>     newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])]
>  
> is to just use the built-in zip:
>
>     newlist = [a+b for a,b in zip(l1[:-1], l1[1:])]
>   
And then there's good old reduce which sadly is going to be harder to 
access in Python 3:

v = [1,2,3,4]
m = []
reduce(lambda x,y,m=m: (m.append(x+y), y)[1], v)
print m # [3, 5, 7]

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239



More information about the Tutor mailing list