Yeah, you could do that, but it was quite a revelation when I discovered itertools, and I'm just trying to share the love.

---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com

(Sent from my iPod - please allow me a few typos: it's a very small keyboard)

On 14 Dec 2008, at 14:46, "Paul McGuire" <[email protected]> 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:])]

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


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to