5

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?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Sumeet Masih
  • 597
  • 1
  • 8
  • 22
  • Possible duplicate of [Pythonic way to split a list into first and rest?](https://stackoverflow.com/questions/3513947/pythonic-way-to-split-a-list-into-first-and-rest) – DYZ Mar 06 '18 at 05:12

1 Answers1

12

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]
falsetru
  • 357,413
  • 63
  • 732
  • 636