Python contains many inbuilt and open-source libraries and modules that are used to accomplish various simple to difficult tasks by utilizing their functions or methods. One of the inbuilt functions, “zip()”, is used to perform various tasks such as iterating over iterable, unpacking iterables, combining multiple iterables, and creating zip iterators.
This write-up will provide you with a detailed guide on Python “zip()” function using numerous examples and via the following content:
- What is the “zip()” Function in Python?
- Working of “zip()” Function in Python
- Using the “zip()” Function With Different Iterable Item
- Using the “zip()” Function to Unzip the Input Iterables
What is the “zip()” Function in Python?
The “zip()” function in Python accepts the zero, single or multiple iterators as an argument and returns the zip iterator object by aggregating them into a tuple. Here, the corresponding items from the given iterables are grouped/paired together as tuples. The syntax of the “zip()” function is shown below:
In the above syntax, the “iterables” specifies the built-in or user-defined iterable such as a list, tuple, dict, or string. This function retrieves an iterator of tuples according to the iterable objects.
Example 1: Working of Python “zip()” Function
The following code shows the working of the “zip()” function in Python:
tuple2 = ("Lily", "Tim", "Jon")
zip_obj = zip(tuple1, tuple2)
print(zip_obj, '\n')
print(tuple(zip_obj))
In the above code:
- The “tuple1” and “tuple2” are initialized in the program.
- The “zip()” function takes the multiple input iterator as an argument and retrieves the zip object or iterator of the tuple.
- The zip object is passed to the “tuple()” function to convert the zip object into a tuple.
Output
The zip object or the tuple iterator is shown in this snippet.
Example 2: Using “zip()” Function With Different Iterable Item
If one tuple contains more items than the other initialized tuple. Then these extra elements are ignored. Here is an example code:
tuple2 = ("Lily", "Tim", "Jon")
zip_obj = zip(tuple1, tuple2)
print(zip_obj, '\n')
print(tuple(zip_obj))
In the above code:
- The “zip()” method takes the multiple iterable with different lengths as an argument and combines the iterable element.
- This method retrieves the zip object by ignoring the extra element of the input iterable.
Output
Example 3: Using “zip()” Function to Unzip the Input Iterables
The following code is used to unzip the input iterable:
list2 = [33, 44, 35]
result_list = list(zip(list1, list2))
print(result_list, '\n')
value1, value2 = zip(*result_list)
print(value1, '\n')
print(value2)
In the above code:
- The “list1” and “list2” sequences are initialized in the program.
- The “zip()” function returns the zip object and passes to the “list()” function to return the list of tuples.
- The “zip()” function takes the list of tuples and unpacks the iterator.
Output
The input iterable has been unpacked successfully.
Example 4: Using the “zip()” Function With Zero or One Iterable
Let’s overview the below code:
zip_obj = zip(tuple1)
print(zip_obj, '\n')
print(tuple(zip_obj))
In this code:
- The “zip()” function takes the one iterable as an argument and retrieves the iterator of the first tuple.
- The returned iterator is transformed into a tuple utilizing the “tuple()” function.
Output
The zip object and iterable of the tuple have been returned successfully.
Now, we can call the zip() function without taking an iterable as an argument. Here is an example code:
print(zip_obj, '\n')
print(tuple(zip_obj))
Here in this code:
- The “zip()” function does not take any argument and retrieves an empty iterator object.
- The iterator object is then converted into an empty/blank tuple utilizing the “tuple()” function.
Output
The empty iterator has been returned successfully.
Conclusion
In Python, the “zip()” function combines two or more iterable into a single iterable and retrieves the iterables of the tuple. Here, the corresponding items from the given iterables are paired/grouped together as tuples. This Python write-up delivered a complete guide on Python’s “zip()” function using multiple examples.