+ 2

Python

a,b,*c = [1,2,3,4] Print(c) Output: [3,4] Can someone tell what exactly is happening in this code? If I remove * it shows error, what * actually does?

3rd Dec 2021, 8:18 PM
Kota Charitha
5 odpowiedzi
+ 3
a, b will be get stored 1,2 respectively and remaing all will be stored in c as list data. edit: another example: a, b, *c, d = [1,2,3,4,5,6,7,8,9] print(*c) #output 3 4 5 6 7 8 # d stores 9 hope it helps.....
3rd Dec 2021, 8:24 PM
Jayakrishna 🇮🇳
+ 2
c gets the remaining values so just look at the position of a and b..we know a is 1 and b is 2.. *c takes everything else after. If we move c to the middle c takes 2 and 3.. move it to the start it will take 1 and 2
4th Dec 2021, 1:10 PM
Jamari McFarlane
Jamari McFarlane - avatar
+ 1
3rd Dec 2021, 8:29 PM
Kota Charitha
+ 1
Google for Python Iterable Unpacking good link for in depth explanation https://stackabuse.com/unpacking-in-python-beyond-parallel-assignment/
4th Dec 2021, 3:08 AM
Bob_Li
Bob_Li - avatar
+ 1
Thank you Bob_Li
4th Dec 2021, 10:13 AM
Kota Charitha