Python zip function returns a zip object that is an iterator of tuples. A tuple is a data type in Python that is used to store the collection of data in a single variable. These are enclosed with round brackets. The data stored in a tuple is ordered and mutable, that is, it can be changed once the tuple has been created. The data items stored in a tuple are separated by a comma (,).
zip () is a built-in function provided in Python. A function is a set of instructions that returns a value. A built-in function can be called anywhere in the code. Iterators in Python are objects that can be iterated. Thus, these objects return data upon iteration one at a time and can be called an iterator if you can receive an iterator from it. Thus, data types such as lists, strings, tuples, etc., are iterables.Â
Using the zip (), the tuple that is created contains the items which are created when each passed iterator is paired together. If the length of the passed iterators that form the terms of the tuple is different, then the length of the new iterator is defined by the one that contains the least item.Â
Syntax of a Python zipÂ
zip (iterator1, iterator2, iterator3, …) |
Thus, with the zip () function, one can convert the iterables in the form of a tuple. There can be zero or more than zero iterables used in a Python zip () function.Â
Python Zip Parameters
The parameters that are passed in the zip () function are iterator objects that are to be joined together. Thus, the interator1, itertaor2, iterator3, etc., are the parameters of the Python zip () function.Â
Return Value from python zip function
The Python zip function returns an iterator of tuples. The tuple is based on the objects that are iterated using the zip () function. Here are different cases of return values that one can obtain from a zip () function.Â
- If no parameters are passed in the zip () function, then it will return an empty iterator.Â
- If only one parameter is passed in the function, then the iterators of tuples that are returned will have tuples carrying only one element.Â
- If two or more parameters are passed in the Python zip () function, then the iterator of tuples will have elements from all the iterables.
One thing to note here is that if, for example, two iterables are passed in a zip () function, and both the iterables have a different number of elements or are of different lengths, then the iterator returned will contain the least number of elements. That is, if one iterable contains 5 elements and another contains 3, then the tuple returned will have three elements. This happens because the iterator stops when the iterable with the least values is exhausted. Â
Zip Python Example
Example 1: A Python program using python zip function
# initiate an iterable, here is a list
fruits = [‘apple’, ‘banana’, ‘orange’, ‘blueberry’]
flowers = [‘rose’, ‘sunflower’, ‘tulips’, ‘lily’]
# when no parameters are passed
result = zip ()
# first the list needs to be converted to an iterator
list_1 = list (result)
print (list_1)
# we now pass the iterables in zipping
result = zip (fruits, flowers)
# the iterator is now converted to a set
set_1 = set (result)
print (set_1)
Output:
[]
{(‘apple’, ‘rose’), (banana, sunflower), (orange, tulips), (blueberry, lily)}
Example 2: A Python program that uses different numbers of iterable elements.Â
names = [‘Rohan’, ‘Akshay’, ‘Priya’, ‘Neha’]
roll_number = [24, 78, 66]
result = zip (names, roll_number)
# the result is converted to a set
set_1 = set (result)
print (set_1)
Output:
{(‘Rohan’, 24), (‘Akshay’, 78), (‘Priya’, 66)}
Notice that once all the three elements of the shortest iterable are exhausted, the zip () function stops returning values.Â
Example 3: A Python program that unzips the value using the zip function python
The operator * is used to unzip a list. The example below shows how it is used.Â
fruits = [‘apple’, ‘banana’, ‘orange’, ‘blueberry’]
flowers = [‘rose’, ‘sunflower’, ‘tulips’, ‘lily’]
result = zip (fruits, flowers)
list_1 = list (result_
print (list_1)
fr, fl = zip (*list_1)
print (fr, fl)
Output:
{(‘apple’, ‘rose’), (banana, sunflower), (orange, tulips), (blueberry, lily)}Â
(‘apple’, ‘banana’, ‘orange’, ‘blueberry’)
(‘rose’, ‘sunflower’, ‘tulips’, ‘lily’)
FAQs on Python Zip
1. What is zip () used for in Python?
Answer: The Python zip () function is used to aggregate the iterables into one tuple. Â
2. Can you zip three lists in Python?
Answer: Yes, the zip () function can be used to aggregate multiple lists. Thus, three or more lists can be joined together and returned in the form of tuples with their elements iterated.Â
3. What is NumPy zip?
Answer: NumPy zip is the function that is used to zip, or join, variables together. It can iterate objects such as array, dictionary, string, list, etc.Â
4. How can one zip a file in Python?
Answer: To zip a file in Python, the following syntax is used.Â
Zipfile.write (filename, arcname = None, compress_type = None)
Leave a Reply