Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - asterisk when zipping

Tags:

python

I'm a newbie with python and I don't understand why doesn't the following work:

ea = zip([[200, 1], [10, 1]])

since I'm getting

[([200, 1],), ([10, 1],)]

while I should add an asterisk like

ea = zip(*[[200, 1], [10, 1]])

to obtain the result I want, i.e.

[(200, 10), (1, 1)]

I thought * was meant to convert the list to a tuple, what am I getting wrong?

like image 389
Dean Avatar asked Oct 28 '25 06:10

Dean


1 Answers

If you have time you can read this post, it's good resource to understand how * works in Python.

The asterisk in Python unpacks arguments for a function call, please refer to here

z = [4, 5, 6]
f(*z)

will be same as:

f(4,5,6)

** in dictionary does similar work as * in list.

like image 153
Andreas Hsieh Avatar answered Oct 31 '25 04:10

Andreas Hsieh