kj wrote:
Is there a special pythonic idiom for iterating over a list (or
tuple) two elements at a time?

I mean, other than

for i in range(0, len(a), 2):
    frobnicate(a[i], a[i+1])

?

I think I once saw something like

for (x, y) in forgotten_expression_using(a):
    frobnicate(x, y)

Or maybe I just dreamt it!  :)

Thanks!
I saw the same thing, forgot where though. But I put it in my library. Here it is:

# x.py
import itertools

def pairs(seq):
    is1 = itertools.islice(iter(seq), 0, None, 2)
    is2 = itertools.islice(iter(seq), 1, None, 2)
    return itertools.izip(is1, is2)

s = range(9)
for x, y in pairs(s):
    print x, y

[EMAIL PROTECTED]> python x.py
0 1
2 3
4 5
6 7
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to