I'd like to iterate over lists in python two by two. E.g.
l = [1, 2, 3, 4, 5, 6, 7, 8]
for a, b in magic_function_I_always_wanted(l)
print(a, b)
>>> 1 2
>>> 2 3
>>> 3 4
>>> 4 5
>>> etc...
I know you can do this in python as well as nearly all languages, I would just like a version which isn't terrible.
I'm curious about all languages but personally care most about python. If there's a way to do this nicely in python (something I haven't thought of which uses tons of izip
or something else equally wacky) please answer!
Edits:
This is not a duplicate of the posted question, since that question asks for l -> (l0, l1), (l2, l3), etc... which is easy. I'm asking for l -> (l0, l1), (l1, l2), etc...