List comprehension with multiple variables
python
Solution
You can use the grouper recipe from the itertools documentation:
>>> from itertools import izip_longest
>>> [x + y for x, y in grouper(2,q)]
[3, 7]
The source code for the recipe is this:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
Problem
Is there any way to dequeue two or more items in list comprehension. I.e. I'd like to do something like this: ``` >>> q = [1, 2, 3, 4] >>> r = [x+y for x, y in q] ** This does not work though :( ** ``` Expected: ``` >>> r [3, 7] ```