Updated March 28, 2023
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, an iterator over the results, whether 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. Suppose lengths of passed iterators differ, length if the iterator decides the new 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 of implementing 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="") print (mapping)
Explanation: In the above code, the lists that are to be associated with each other are declaring. The 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) print(res_list) 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) print(result) result_tuple = tuple(result) print("Zipped Tuples: ",result_tuple) print("Äddress 1: ",result_tuple, result_tuple) t, n = zip(*result_tuple) print("Texts: ",t) print("Numbers: ",n)
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 the 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 the zip function.
numList = [1558, 1884, 1111] strList = ['one', 'two', 'three'] A_output = zip(numList, strList) print(A_output) 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 –