
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Zip Function
The zip() function in Python is used to map elements from multiple iterables at corresponding index positions. It returns an iterator of tuples, where each tuple contains elements from the input iterables at the same index.
Suppose the lengths of the input iterables are not the same. In that case, the zip() function will stop pairing once the shortest iterable is exhausted, ignoring any extra elements from the longer iterables. Following is the syntax of the zip() function in Python ?
zip(iterable1, iterable2)
It accepts iterables as a parameter and returns a single iterable.
Using zip() with list
list is one of the built-in data types in Python. A Python list is a sequence of comma-separated items, enclosed in square brackets []. The items in a Python list need not be the same data type.
The zip() function maps two or more lists into a single iterable. The elements of the corresponding positions are paired together and return iterable in the form of a tuple.
Example
In the following example we have zipped the two lists using the zip() function ?
my_list1 = ['one', 'two', 'three', 'four'] my_list2 = [1, 2, 3, 4] print(list(zip(my_list1,my_list2)))
Following is the output of the above code ?
[('one', 1), ('two', 2), ('three', 3), ('four', 4)]
Using zip() with dictionary
In Python, a dictionary is a built-in data type that stores data in key-value pairs. It is an unordered, mutable, and indexed collection. Each key in a dictionary is unique and maps to a value. Python's dictionary is an example of a mapping type. To establish a mapping between a key and a value, the colon (:) symbol is put between the two. The zip() function in Python is used to map two lists into a dictionary.
Example
In the following example we have converted two lists to a dictionary using the zip() function ?
my_list1 = ['Course 1', 'Course 2', 'Course 3'] my_list2 = ['Python','C++','Java'] new_dict = dict(zip(my_list1,my_list2)) print("Dictionary : ",new_dict)
Output
Following is the output of the above code ?
Dictionary : {'Course 1': 'Python', 'Course 2': 'C++', 'Course 3': 'Java'}
Using zip() with unequal size
If the given iterables have different lengths, the zip() function will only iterate up to the length of the shortest iterable, pairing the elements of each iterable until the shortest one is exhausted.
Example
Here, we have grouped the unequal-length lists using the zip() function ?
students = ["John", "Rohan", "Ram", "Jersey","Raju", "Neha"] marks = (95,85,70,65) #lists along with the 'ages' tuple zipped_result = zip(students, marks) # Print the zipped object print(list(zipped_result))
Output
Following is the output of the above code ?
[('John', 95), ('Rohan', 85), ('Ram', 70), ('Jersey', 65)]
Unzipping using zip()
Unzipping using zip() means converting the zipped values back to the individual iterables. * operator is used unzip.
Example
Here, we have unzipped using zip() function along with * operator ?
students = ['Rani', 'Neha', 'Ram', 'Sai', 'Rohan'] roll_no = [24,25,26,27,28] marks = [85, 75, 82, 95, 60] zip_list = list(zip(students, roll_no, marks)) print("Zipped List :", zip_list) # unzipping values students, roll_no, marks = zip(*zip_list) print("Students :", students) print("Roll No :", roll_no) print("Marks :", marks)
Following is the output of the above code ?
Zipped List : [('Rani', 24, 85), ('Neha', 25, 75), ('Ram', 26, 82), ('Sai', 27, 95), ('Rohan', 28, 60)] Students : ('Rani', 'Neha', 'Ram', 'Sai', 'Rohan') Roll No : (24, 25, 26, 27, 28) Marks : (85, 75, 82, 95, 60)