+ 2
Meaning of adding an asterisk before a variable in python
In the code below the variable c has an asterisk before it. Printing the variable c returns a list, similarly checking it's type also return class list. what exactly is the function of asterisk here?. *c,d="2","3" print(d) print(c) print(type(d)) print(type(c)) 3 ['2'] <class 'str'> <class 'list'>
1 ответ
+ 9
in simple words. * here means "take everything what is not taken".
your example is not that obvious. Assume we have such a snippet:
a, *b, c = 1, 2,3,4,5,8
here a takes the fitst value from the right part, c = the last value, and *b is interpreted the last and takes everythng left as a list:
>>> a, *b, c = 1, 2,3,4,5,8
>>> a
1
>>> c
8
>>> b
[2, 3, 4, 5]
>>> print(*b)
2 3 4 5