Iterate through list two items at a time

Gabriel Genellina gagsl-py at
Wed Jan 3 03:57:13 CET 2007

At Tuesday 2/1/2007 22:57, Dave Dean wrote:

>   myList = [1,2,3,4,5,6]
>I'd like to be able to pull out two items at a time - simple examples would
>Create this output:
>1 2
>3 4
>5 6

for x in b:
    print x,y

for x,y in ((item, for item in b):
    print x,y

>Create this list:
>[(1,2), (3,4), (5,6)]

[(item, for item in b]

Note that they don't behave the same at the corner cases (empty list, 
single item, odd length...)

Gabriel Genellina
Softlab SRL 


Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the Python-list mailing list