Python zip() function
Published February 17, 2022Python 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" ] |
Output
{('cerial', 'maize'), ('drink', 'coffee'), ('Fruit', 'Orange'), ('drug', 'Moderna vaccine')} |
Article Contributed By :
|
|
|
|
225 Views |