Zip Function

Introduction to zip function

It is the function which is used to aggregate elements from different iterables into one iterables in such a form that iterables with the same index is merged as one.

Let’s look through example, will give better intuition where to use zip.

Suppose, we are having two lists and we are aggregating them into one list. Let’s see how.

How to zip and access the elements of the zipped list?

Next() method

In zip function, we also have the next method to access the next value. As we have already seen it in generators, enumerate.

In this example, it is showing some error because in the previous step in which we are using ‘for in’ loop to access values and we are reaching till the end. So the next method is not able to detect any value after that.

Zipping for different lengths of iterables

If we are zipping different length iterables then, it will aggregate till the length of shorter iterable.

Unzipping

In this unzipping, it is just the opposite of what we have done in zipping.

Close Menu