Last answered:

30 Apr 2024

Posted on:

03 Feb 2022

0

* in zip(*variable.items())

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

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

Super learner
This user is a Super Learner. To become a Super Learner, you need to reach Level 8.
Posted on:

10 Dec 2023

0

I'd like to share an additional information about * that I hope it will be useful:

* is an unpacking operator that is used with iterables like lists: 
if we have a list called L we can unpack its elements and print them 

L = [1, 2, 3, 4, 5]
print(*L)

This will print the list without brackets and with spaces between each element unlike if i printed it this way:

print[L]

 This will print the list with brackets and seperated with commas.

Also the * operator can be used to unpack an iterable in a function arguments:

L = ['365', 'data', 'science']

def some_func(a, b, c):
    print(a, b, c)
    
some_func(*L)

the * operator can also be used with functions for accepting an arbitrary number of arguments:

def some_func(*nums):  # nums will be a tuple that holds the arguments
    for num in nums:
        print(num)

some_func(5, 6, 7, 8)


In case of zip function it will receive the list of tuples of key value pairs of the dictionary and the * operator will unpack this list of tuples to be individual tuples.

Posted on:

30 Apr 2024

0

Zip is a useful function that allows you to combine lists.
In the other hand, Unzip is a function to output the separated lists.


Zip with * is the Unzip


Submit an answer