Assign multiple values of a list

Simply type it out:

>>> a,b,c,d = [1,2,3,4]
>>> a
1
>>> b
2
>>> c
3
>>> d
4

Python employs assignment unpacking when you have an iterable being assigned to multiple variables like above.

In Python3.x this has been extended, as you can also unpack to a number of variables that is less than the length of the iterable using the star operator:

>>> a,b,*c = [1,2,3,4]
>>> a
1
>>> b
2
>>> c
[3, 4]

Leave a Comment