How to iterate over a sequence, for example a list
, and return a sub-sequence, for example a tuple
? Sometimes this is referred to as "chunks" or "chunking".
for (a, b) in [1, 2, 3, 4, 5]:
print('%s %s' % (a, b))
would print
1 2
2 3
3 4
4 5
The code example is not valid Python code. But it expresses what I want.
Can this be done within one expression (e.g. within a lambda
)?