Last answered:

12 Nov 2023

Posted on:

03 Feb 2022

0

* in zip(*variable.items())

Hello What is the purpose of * in zip(*capitals.items())? Thank you!

4 answers ( 0 marked as helpful)
Instructor
Posted on:

04 Feb 2022

1

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

Posted on:

21 Mar 2023

0

hi Hristina, may i understand what is the meaning or function of * in the above syntax? thanks!

Instructor
Posted on:

21 Mar 2023

1

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

Posted on:

12 Nov 2023

1

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`.

Submit an answer