[Tutor] Working with lists

Rich Lovely roadierich at googlemail.com
Mon Dec 15 02:42:47 CET 2008


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" <ptmcg at austin.rr.com> 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  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list