* in zip(*variable.items())
Hello What is the purpose of * in zip(*capitals.items())? Thank you!
Hey,
Thank you for your question!
As Giles demonstrates, the output of capitals.items()
is a list of tuples, where each tuple stores a key and its corresponding value. For example, a key could be France
and its value would be Paris
.
The purpose of zip(*capitals.items())
is to unzip these key:value pairs into two variables - x
and y
. x
would be the one storing the keys, namely France, Spain, etc, whereas y
would be the one storing the values, namely Spain, Madrid, etc. We have separated the key:value pairs.
Hope this helps!
Kind regards,
365 Hristina
hi Hristina, may i understand what is the meaning or function of * in the above syntax? thanks!
Hey Chan,
Thank you for reaching out!
The difference between zip(...)
and zip(*...)
is that the former function zips content while the latter unzips it. Take a look at the example below.
I've defined two lists--one storing countries
and the other one storing the corresponding capitals
. The variable joined
is a list of tuples with each tuple storing a country and its corresponding capital--the content is zipped. The variables a
and b
, on the other hand, store the unzipped content of the joined
variable.
Hope this helps!
Kind regards,
365 Hristina
Hello Hristina, just a note that there is an error in your print statement - instead of printing variable `x`, which is undefined, it should print the variable `joined`.