zip() in Python

30 Sep, 2021
Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. 
It is used to map the similar index of multiple containers so that they can be used just using a single entity. 
Syntax :  zip(*iterators) 
Parameters : Python iterables or containers ( list, string etc ) Return Value : Returns a single iterator object, having mapped values from all the containers.
Python3
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]roll_no = [ 4, 1, 3, 2 ] # using zip() to map valuesmapped = zip(name, roll_no) print(set(mapped))
Output:
{('Shambhavi', 3), ('Nikhil', 1), ('Astha', 2), ('Manjeet', 4)}
Python3
names = ['Mukesh', 'Roni', 'Chari']ages = [24, 50, 18] for i, (name, age) in enumerate(zip(names, ages)):    print(i, name, age)
Output:
0 Mukesh 24
1 Roni 50
2 Chari 18
Python3
stocks = ['reliance', 'infosys', 'tcs']prices = [2175, 1127, 2750] new_dict = {stocks: prices for stocks,            prices in zip(stocks, prices)}print(new_dict)
Output:
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}
Unzipping means converting the zipped values back to the individual self as they were. This is done with the help of “*” operator.
Python3
# Python code to demonstrate the working of# unzip # initializing listsname = ["Manjeet", "Nikhil", "Shambhavi", "Astha"]roll_no = [4, 1, 3, 2]marks = [40, 50, 60, 70] # using zip() to map valuesmapped = zip(name, roll_no, marks) # converting values to print as listmapped = list(mapped) # printing resultant valuesprint("The zipped result is : ", end="")print(mapped) print("\n") # unzipping valuesnamz, roll_noz, marksz = zip(*mapped) print("The unzipped result: \n", end="") # printing initial listsprint("The name list is : ", end="")print(namz) print("The roll_no list is : ", end="")print(roll_noz) print("The marks list is : ", end="")print(marksz)
Output: 
The zipped result is : [('Manjeet', 4, 40), ('Nikhil', 1, 50), 
('Shambhavi', 3, 60), ('Astha', 2, 70)]


The unzipped result: 
The name list is : ('Manjeet', 'Nikhil', 'Shambhavi', 'Astha')
The roll_no list is : (4, 1, 3, 2)
The marks list is : (40, 50, 60, 70)
There are many possible applications that can be said to be executed using zip, be it student database or scorecard or any other utility that requires mapping of groups. A small example of scorecard is demonstrated below. 
Python3
# Python code to demonstrate the application of# zip() # initializing list of players.players = ["Sachin", "Sehwag", "Gambhir", "Dravid", "Raina"] # initializing their scoresscores = [100, 15, 17, 28, 43] # printing players and scores.for pl, sc in zip(players, scores):    print("Player :  %s     Score : %d" % (pl, sc))
Output: 
Player :  Sachin     Score : 100
Player :  Sehwag     Score : 15
Player :  Gambhir     Score : 17
Player :  Dravid     Score : 28
Player :  Raina     Score : 43
varshagumber28
kumar_satyam
Python-Built-in-functions
python-list
python-list-functions
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON