Home » zip() in Python | Python zip() Function with Examples

zip() in Python | Python zip() Function with Examples

by Online Tutorials Library

Python zip() Function

Python zip() function returns a zip object, which maps a similar index of multiple containers. It takes iterables (can be zero or more), makes it an iterator that aggregates the elements based on iterables passed, and returns an iterator of tuples.

Signature

Parameters

iterator1, iterator2, iterator3: These are iterator objects that joined together.

Return

It returns an iterator from two or more iterators.

Python zip() Function Example 1

The below example shows the working of zip().

Output:

[]  {(5, 'five'), (4, 'four'), (6, 'six')}  

Explanation: In the above example, we have initialized the lists, i.e., numList (contains integer values) and strList (contains string values) and map the values by using zip().

In the first case, it converts iterator to a list and prints resultList that contains no values.

Then, two iterables are passed in the zip(), i.e., numList, strList and convert iterator to set and return its values.

Python zip() Function Example 2

The below example unzip the values using zip().

Output:

[('x', 6), ('y', 2), ('z', 8)]  c = ('x', 'y', 'z')  v = (6, 2, 8)  

You may also like