Python zip() function

Published February 17, 2022

Python includes several built-in looping functions that let programmers iterate over specific data or code. The zip() function in Python is one of these functions that you can use to create an iterator that merges elements from different data sources into one. The zip object returned by this function stores the iterables in pairs. These iterables include sets, dictionaries, tuples, and lists.

The syntax of the Zip() Function
The zip() function is written as follows:

zip(iterable1, iterable2, ...)

The zip() function works with two or more iterables, and as a result, returns a list of tuples as pairs.

Example

In the following example, we are going to match the category name with the category data.

category_name = [ "Fruit", "cerial", "drink", "drug" ]
category_data = [ "Orange", "maize", "coffee", "Moderna vaccine" ]
x = zip(category_name, category_data)
print(set(x))


Output

{('cerial', 'maize'), ('drink', 'coffee'), ('Fruit', 'Orange'), ('drug', 'Moderna vaccine')}

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

225 Views