[Tutor] Working with lists
Paul McGuire
ptmcg at austin.rr.com
Sun Dec 14 15:46:17 CET 2008
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:])]
since you can be sure that l1[:-1] and l1[1:] will always be the same
length, so there is no need for a fill value (one of the enhancements you
get when using itertools.izip).
I often use zip this way when I need to get each item and its next-highest
neighbor from a list.
-- Paul
More information about the Tutor
mailing list