[Tutor] Unpack from list
Mark Lawrence
breamoreboy at yahoo.co.uk
Mon Jul 13 14:27:13 CEST 2015
On 13/07/2015 13:03, Sunil Tech wrote:
> Hi Tutor,
>
>
> [a,b,c] = [1,2]
> this will give
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: need more than 2 values to unpack
>
> But is there any chance, if there are 3 values on left hand side and 2
> values on right side, to add an empty value to the left side dynamically?
>
You need "Extended Iterable Unpacking" as described in
https://www.python.org/dev/peps/pep-3132/
<quote>
An example says more than a thousand words:
>>> a, *b, c = range(5)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]
<quote>
Hence:-
>>> a,b,*c=[1,2]
>>> a,b,c
(1, 2, [])
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list