Assign multiple values ββto a list
I'm curious to know if there is a "poofy" way to assign values ββin a list to elements? To be clearer, I am asking for something like this:
myList = [3, 5, 7, 2] a, b, c, d = something(myList)
So to:
a = 3
b = 5
c = 7
d = 2
I'm looking for any other better option than doing it manually:
a = myList[0]
b = myList[1]
c = myList[2]
d = myList[3]
+3
source to share
2 answers
Just enter it:
>>> a,b,c,d = [1,2,3,4]
>>> a
1
>>> b
2
>>> c
3
>>> d
4
Python uses assignment unpacking
when you have iterable
assigned to multiple variables like the one above.
As Python3.x
it has been extended, as you can also unpack a few variables, which are less than the length iterable
, the operator using the stars:
>>> a,b,*c = [1,2,3,4]
>>> a
1
>>> b
2
>>> c
[3, 4]
+4
source to share