Python 3.x allows dumping excess return in a parallel assignment with *
>>> a, b, *args = range(4)
>>> a
0
>>> b
1
>>> args
[2, 3]
But 2.x doesn't:
>>> a, b, *args = range(4)
File "<stdin>", line 1
a,b,*args = range(4)
^
SyntaxError: invalid syntax
Is there some future import that can make this statement compatible with Python 2.x?
AFAIK this feature is not available in Python 2, if you really need something like this – just write utility function
def unpack(iterable, elements_count):
iterator = iter(iterable)
for _ in range(elements_count):
yield next(iterator)
# maybe use `list` or leave `iterator`-object
yield tuple(iterator)
Then
a, b, args = unpack(range(4), 2)
will give an expected behaviour
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With