looping throuhg a list 2 items at a time

Emile van Sebille emile at fenx.com
Wed Apr 10 19:40:43 EDT 2002


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