How to Use Python zip Function
In this article, we will discuss zip() function in python, and how to use it with the help of examples. Later in the article, we will also discusss how to use zip_longest function to get all the element of the iterable and finally how to unzip the iterable.
When we have multiple files to share, we zip them all together in one folder. zip() function in PythonPython performs the same thing; it brings the elements of the same index from multiple iterable objects together as the element of the same tuple.
In PythonPython, list, tuples, sets, dictionary, etc. are known as iterable objects.
Also Read: List vs. Tuple
Now, without further delay, letβs learn more about zip() function and how to use it, and finally, we will also learn how to unzip.
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is zip() function in Python?
zip() function is a built-in function in PythonPython that takes any number of iterable (list, set, dictionary, tuples) as an argument and returns a single iterator object which is a tuple (zip object).
- zip() function is used to iterate through multiple iterables.
- The output tuple contains the elements from each passed iterable.
- The first elements of all input iterators are zipped together and become the first element of the output tuple.
- Similarly, for the second element, third element, and so on.
- The output tuple contains all the mapped tuples in the same order.
- If the length of the passed iterable is different, then the elements in the longer iterable are left out.
Syntax
zip(iterator_1, iterator_2, iterator_3, β¦)
Parameter:
Iterables: list, set, dictionary, tuple
Return Value
- No iterator returns an empty iterator.
- One iterator returns a tuple having only one element.
- Multiple iterators return an iterator of a tuple having elements from each iterable.
How zip() function works?
- zip() function first calculates the number of iterable passed as an argument.
- Then, find the minimum length of the iterable from the given set of iterable
- Creates, a tuple of length equal to the minimum length, that contains the elements from each iterable.
- Then, it creates an iterator for each iterable passed to it.
- It creates an iterator for each iterable by taking element from each iterable and aggregate them together.
- zip(x,y) = [(x0, y0), (x1, y1), (x2, y2),β¦ ]
- It stops, at the minimum length of the iterable.
- It creates an iterator for each iterable by taking element from each iterable and aggregate them together.
- Then, it creates an iterator for each iterable passed to it.
- Return the iterator as a tuple.
Now, letβs take some examples:
Example -1: No iterable is passed in zip() function.
# use zip() function without passing an argument
zipped = zip()
#print the list
list(zipped)
Output
Example -2: One iterable is Passed
# use zip() function with one argument
cricketer = ['Dhoni', 'Virat', 'Rohit', 'Sachin', 'Ganguly']zipped = zip(cricketer)
# print the listlist(zipped)
Output
Example β 3: Two iterable is passed
# use zip() function with two arguments
cricketer = ['Dhoni', 'Virat', 'Rohit', 'Sachin', 'Ganguly']age = [38, 34, 33, 45, 47]zipped = zip(cricketer, age)
# print the listlist(zipped)
Output
Example β 4: Passing Argument of Different Lengths
# use zip() function with different number of iterable elements# here the length of all three lists are different
cricketer = ['Dhoni', 'Virat', 'Rohit', 'Sachin', 'Ganguly']age = [38, 34, 33, 45]century = [25, 35]zipped = zip(cricketer, age, century)
# print the listlist(zipped)
Output
If we see the above example, the length of the output tuple is equal to the length of the shortest tuple. But, if we want all the elements from all the iterable, then we can use zip_longest.
Letβs see how we can use zip_longest.
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Example β 5: Using zip_longest to get all the value from the zip() function
# use zip_longest() function with different number of iterable elements# here the length of all three lists are different#import zip_longest() from itertools
from itertools import zip_longest
cricketer = ['Dhoni', 'Virat', 'Rohit', 'Sachin', 'Ganguly']age = [38, 34, 33, 45]century = [25, 35]zipped = zip_longest(cricketer, age, century)
# print the listlist(zipped)
Output
In the above example, we get all the elements of all iterable. But if we want to replace the None value from the output, we can simply use fillvalue in the zip_longest function.
Till now, we have seen a lot of examples of how to zip differently iterable. But how will you unzip the iterable?
It is quite simple; you can use the zip() function with the * operator to unzip the iterable.
Letβs see the example of how to unzip.
Example -6: Unzip the iterable
# use zip(*arg) function to unzip the iterables
cricketer = ['Dhoni', 'Virat', 'Rohit', 'Sachin', 'Ganguly']age = [38, 34, 33, 45]century = [25, 35]cricket= list(zip(cricketer, age, century))
# print the listprint(cricket)
#unzip cricket into seprate iterablescricketer_unzip, age_unzip, century_unzip = zip(*cricket)
#print the list of iterablesprint(cricketer_unzip)print(age_unzip)print(century_unzip)
Output
Recursion Function in Python | count() Function in Python |
len() Function in Python | float() Function in Python |
range() Function in Python | lambda() Function in Python |
Conclusion
In this article, we have discussed zip() function in python, and how to use it with the help of examples. Later in the article, we have also discussed how to use zip_longest function to get all the element of the iterable and finally how to unzip the iterable.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio