Imagine I have a list like this:
a = [1,2,3,4,5,6]
Using a lambda function, I would like to return two elements at a time, so the result would be:
res = [[1,2], [2,3], [3,4], [4,5], [5,6]]
Any suggestions?
Imagine I have a list like this:
a = [1,2,3,4,5,6]
Using a lambda function, I would like to return two elements at a time, so the result would be:
res = [[1,2], [2,3], [3,4], [4,5], [5,6]]
Any suggestions?
I recommend using a generator.
you can always grab the list if you want:
One option:
This avoids creating a copy of your original list, which may be relevant if it's very big.
For arbitrary
n
: