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
371k68 gold badges768 silver badges659 bronze badges
asked Mar 6, 2018 at 4:36
1

1 Answer 1

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]
answered Mar 6, 2018 at 4:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.