The purpose of zip() is to map the similar index of multiple containers so that they can be used just using as 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.
# Python code to demonstrate the working of # zip() # initializing lists name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ] roll_no = [ 4, 1, 3, 2 ] marks = [ 40, 50, 60, 70 ] # using zip() to map values mapped = zip(name, roll_no, marks) # converting values to print as set mapped = set(mapped) # printing resultant values print ("The zipped result is : ",end="") print (mapped) |
Output:
The zipped result is : {('Shambhavi', 3, 60), ('Astha', 2, 70),
('Manjeet', 4, 40), ('Nikhil', 1, 50)}
How to unzip?
Unzipping means converting the zipped values back to the individual self as they were. This is done with the help of “*” operator.
# Python code to demonstrate the working of # unzip # initializing lists name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ] roll_no = [ 4, 1, 3, 2 ] marks = [ 40, 50, 60, 70 ] # using zip() to map values mapped = zip(name, roll_no, marks) # converting values to print as list mapped = list(mapped) # printing resultant values print ("The zipped result is : ",end="") print (mapped) print("\n") # unzipping values namz, roll_noz, marksz = zip(*mapped) print ("The unzipped result: \n",end="") # printing initial lists print ("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)
Practical Applications : There are many possible applications that can be said to be exected 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.
# Python code to demonstrate the application of # zip() # initializing list of players. players = [ "Sachin", "Sehwag", "Gambhir", "Dravid", "Raina" ] # initializing their scores scores = [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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Zip function in Python to change to a new character set
- Python | Zip different sized list
- Python | Zip Uneven Tuple
- Application to get address details from zip code Using Python
- Working with zip files in Python
- Get Zip Code with given location using GeoPy in Python
- Create pandas dataframe from lists using zip
- PYGLET – Creating ZIP Location Object
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

