Iterate through list two items at a time
Gabriel Genellina
gagsl-py at yahoo.com.ar
Tue Jan 2 21:57:13 EST 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
>be:
>Create this output:
>1 2
>3 4
>5 6
b=iter(a)
for x in b:
y=b.next()
print x,y
b=iter(a)
for x,y in ((item, b.next()) for item in b):
print x,y
>Create this list:
>[(1,2), (3,4), (5,6)]
b=iter(a)
[(item, b.next()) 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!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list