looping throuhg a list 2 items at a time

dsavitsk dsavitsk at e-coli.net
Wed Apr 10 20:47:30 EDT 2002


I guess this proves TMTOWTDI.

"Emile van Sebille" <emile at fenx.com> wrote in message
news:%34t8.36244$CU.41218 at rwcrnsc51.ops.asp.att.net...
> Rajarshi Guha
> >   I have a list like this:  l = [1,'s', 2,'t', 3,'d']
> >
> [snip]
> > whereas I want
> >
> > 1 s
> > 2 t
> > 3 d
> >
>
> >>> for x,y in [(x,y) for x,y,z in zip(l,l[1:],[1,0]*len(l)) if z]:
> ...     print x,y
>
>
> Have fun...  ;-)
>
> --
>
> Emile van Sebille
> emile at fenx.com
>





More information about the Python-list mailing list