Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python List Multiple Assignment

How to do Python list multiple assignment in one line.

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

but what should I do to assign rest of the sub array to c

>>> a,b,c = [1,2,3,4,5,6,7,8,9] ##this gives an error but how to ..?
>>> a
1
>>>b
2
>>>c
[3,4,5,6,7,8,9]

how to do this?

like image 889
Sumeet Masih Avatar asked Dec 19 '22 00:12

Sumeet Masih


1 Answers

You can use Extended iterable unpacking: by adding * in front of c, c will catch all (rest) items.

>>> a, b, *c = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a
1
>>> b
2
>>> c
[3, 4, 5, 6, 7, 8, 9]
like image 65
falsetru Avatar answered Dec 27 '22 01:12

falsetru