Introduction to Python Zip Function
Zip function is pretty much the opposite of Lambda functions and Map functions in Python. Zip function aggregates based on iterables. The zip object is returned through a zip() function which is an iterator over the results may it be lists or tuples or any returnable object. This is where pairing together of each passed iterator of the first item and the second item are paired together in each passed iterator. If lengths of passed iterators differ, length if the new iterator is decided by the iterator with least items. In simple terminology, ith elements associate with an ith tuple of each iterable sequence of arguments.
Syntax of Zip Function
Below is the syntax for zip() Function:
The zip function takes two parameters:
- Function: Function that tests if elements of an iterable aggregate-based.
- Iterable: Iterable which is to be filtered could be sets, tuples, lists or containers of any iterators.
Examples to Implement Python Zip Function
Below is the example to implement Python zip() Function:
Consider a program that takes in student’s names and numbers associated with marks.
name = [ "Hemanth", "Anjum", "Ramya", "Saira" ]
id_no = [ 111, 92, 137, 103 ]
score = [ 40, 50, 60, 70 ]
# Mapping values using zip()
mapping = zip(name, id_no, score)
# A set of values print after conversion
mapping = set(mapping)
# resultant values are printed
print ("The zipped values are : ",end="")
Explanation: In the above code, the lists that are to be associated with each other are declaring. Name of the student, identity number, and the marks are the variables. The association link is known through using a zip function. This zip function returns the iterable value The iterable or object value is again converted to a list and displayed.
Consider a program that takes in students’ names and numbers associated with marks.
coordinate_plane = ['x', 'y', 'z']
val = [13, 14, 15]
res = zip(coordinate_plane, val)
res_list = list(res)
c, v = zip(*res_list)
print('c =', c)
print('v =', v)
Zipping values go something like this.
texts = ("Baker Street", "Golden Ratio", "Room")
numbers = (221.5, 1.61803, 237)
result = zip(texts, numbers)
result_tuple = tuple(result)
print("Zipped Tuples: ",result_tuple)
print("Äddress 1: ",result_tuple, result_tuple)
t, n = zip(*result_tuple)
Explanation: Two iterables are defined. They can be anything. For example Tuples or Lists. Here, Tuples are used. In the first one – texts and in the second one – numbers. Baker Street, Golden Ratio, Room are given with respective numbers. The numbers automatically align and match each other by zipping the two tuples together. For Zipping the two, we use zip function When you print the zipped function, it returns an iterable To convert it to a normal format, we use Tuple which prints the result in the Tuple form Each address and its particulate is returned if asked for it by using the indices for instance. We have created and printed three aggregated tuples. Let’s assume that we have a list of tuples we want to unzip to get separate iterables. Searching for a function named unzipped which doesn’t exist perhaps, but there is an unzip operator which is an asterisk. So, after you pass the aggregated tuples and each tuple has elements, and that is printed in the results.
Unzipping Values with Zip Function
The data extraction is done using the same zip function. An asterisk symbol (*) is used in front of the list which gives an unzipped value. Number List and string List can also be unzipped using zip function.
numList = [1558, 1884, 1111]
strList = ['one', 'two', 'three']
A_output = zip(numList, strList)
a, b = zip(*A_output )
print('numList = ', a)
print('strlist = ', b)
Explanation: The two – number list and string list are declared. These lists are first zipped using zip function Iterable value or object at position is printed after being zipped. The zipped values are unzipped using an asterisk (*) which is placed before the zipped result value. The output shows both lists which are separated on their own.
Zip function in Python is an in-built function. It returns an object of the zip or usually called an iterable. Taking iterables can be zero or more. Python zip functions are even used for unzipping the values. Multiple iterables look up, Dictionary traversing is the most common use cases of zip function in Python.
This is a guide to Python Zip Function. Here we discuss Syntax, parameters, the example to implement with proper syntax and respective sample code. You can also go through our other related articles to learn more –