Iterate through list two items at a time

Dave Dean dave.dean at xilinx.com
Tue Jan 2 20:57:18 EST 2007


Hi all,
  I'm looking for a way to iterate through a list, two (or more) items at a 
time.  Basically...

  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

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

I want the following syntax to work, but sadly it does not:
for x,y in myList:
  print x, y

I can do this with a simple foreach statement in tcl, and if it's easy in 
tcl it's probably not too hard in Python.

Thanks,
Dave 





More information about the Python-list mailing list