Python List Multiple Assignment

How to make Python list multiple destinations on one line.

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

but what should I do to assign the rest of the auxiliary array 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 it?

+4
source share
2 answers

You can use Extended iterable unpacking : adding *before c, c will capture all (the rest) of the elements.

>>> a, b, *c = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a
1
>>> b
2
>>> c
[3, 4, 5, 6, 7, 8, 9]
+10
source

First, assign the list like this:

a= [1,2,3,4,5,6,7,8,9]

assign the second element "b" with:

b=a[1]

set the remaining elements to 'c':

c=a[2:9]

then reassign the first element to 'a':

a=a[0]

Here we go.:)

-3
source

Source: https://habr.com/ru/post/1694504/