333

7
+9  Q:

## How do I loop through a Python list by twos?

+10  A:

If you're using Python 2.6 or newer you can use the grouper recipe from the `itertools` module:

``````from itertools import izip_longest

def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
``````

Call like this:

``````for item1, item2 in grouper(2, l):
# Do something with item1 and item2
``````

Note that in Python 3.x you should use `zip_longest` instead of `izip_longest`.

+1 This is the method the documentation itself suggests.
+8  A:

You can use for in range with a step size of 2

``````for i in xrange(0,10,2):
print(i)
``````

Note: Use xrange instead of range because it is more efficient as it generates an iterable object, and not the whole list.
Edit: As @Daniel Stutzbach mentions, in Python 3 `range` generates an iterable object.

+1. It's worth noting that you're describing Python 2's xrange and range. In Python 3, range() generates an iterable object.
+1  A:

If you have control over the structure of the list, the most pythonic thing to do would probably be to change it from:

``````l=[1,2,3,4]
``````

to:

``````l=[(1,2),(3,4)]
``````

``````for i,j in l:
print i, j
``````
It's a property being read in from an external file. I can't control the list and I actually am trying to change it from `l=[1,2,3,4]` to `l=[(1,2),(3,4)]` which is why I'm asking the question
+3  A:

You can also use this syntax (`L[start:stop:step]`):

``````mylist = [1,2,3,4,5,6,7,8,9,10]
for i in mylist[::2]:
print i,
# prints 1 3 5 7 9

for i in mylist[1::2]:
print i,
# prints 2 4 6 8 10
``````

Where the first digit is the starting index (defaults to beginning of list or 0), 2nd is ending slice index (defaults to end of list), and the third digit is the offset or step.

This will work very well, as long as the `list` isn't huge. If the `list` _is_ huge, you're making a copy of it when you use the slicing syntax...
That is correct, sir! Thanks for noting that.
A:
``````nums = range(10)
for i in range(0, len(nums)-1, 2):
print nums[i]
``````

Kinda dirty but it works.

Considering that `range`/`xrange` have built in support for changing the increment value, this solution seems kind of silly.
Umm, that's what the the last parameter to `range` is doing...
It's presumed that nums is a sequence we have no control over. The demonstration really begins subsequent to the first line where I assign `nums` a value.
A:

This might not be as fast as the izip_longest solution (I didn't actually test it), but it will work with python < 2.6 (izip_longest was added in 2.6):

``````from itertools import imap

def grouper(n, iterable):
"grouper(3, 'ABCDEFG') --> ('A,'B','C'), ('D','E','F'), ('G',None,None)"
args = [iter(iterable)] * n

return imap(None, *args))
``````

If you need to go earlier than 2.3, you can substitute the built-in map for imap. The disadvantage is that it provides no ability to customize the fill value.

+1  A:

The simplest in my opinion is just this:

``````data = [1,2,3,4,5,6]
for x, y in zip(data, data[1:]):
print x
print y
``````

No extra imports or anything. And very elegant, in my opinion.

This doesn't quite do what this question asks for.