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
.