Get startedGet started for free

What is a zip object?

1. What is a zip object?

In this lesson we'll cover the zip object and the functionality it provides. So, what does it do?

2. Definition

zip is an object that can combine several iterable objects

3. Definition

into one iterable object.

4. Definition

When we traverse the resulting zip object, we can see that each item represents a tuple containing elements from original iterable objects with the same indices.

5. Example

Let's look at an example. We have three Iterables: a string, a list, and a dictionary. Let's combine these objects using zip() into one zip object.

6. Traversing through a zip object

Since by definition a zip object is an Iterable, we can use it in a for loop and check how each item looks like. So, each item is a tuple containing elements from the original Iterables sharing the same index. Notice that in case of a dictionary passed to a zip() constructor, the keys are kept and the values are ignored.

7. Returning a list of tuples

To wrap these tuples in a list, we can pass the zip object to the list() constructor.

8. zip object as Iterator

A zip object is also an Iterator. That means we can apply the next() function on it to traverse over the constituent tuples. Of course, traversing is possible until a StopIteration error is raised.

9. zip object is expendable

It's important to mention that traversing a zip object can be done only once. It means that we can use it in a for loop only one time. It happens because, as we learned from the lesson on Iterables, a zip object is both an Iterable and an Iterator.

10. zip object is expendable

Using it in a for loop the second time will not retrieve any elements. The same applies to the list() constructor when we want to create a list from a zip object.

11. 'zip' object is expendable

Calling it the second time will result in an empty list.

12. Unequal Iterable sizes

What happens when one of the iterable objects contains more elements than another?

13. Unequal Iterable sizes

For example, let's unwrap the abbreviation here. Now the title definitely represents a much longer Iterable than the other two. Now, let's combine our Iterables.

14. Traversing through the 'zip' object

By traversing through the created zip object, we can see that the amount of resulting items corresponds to the length of the "shortest" Iterable. In this case, there are two: the list of villains and the dictionary of turtles.

15. Reverse operation

Python also allows to perform a reverse operation to unzip a list containing tuples. Actually, it's also called zip. Assume, we have the following input. If we use zip() but with an asterisk before the argument, we will get the following output. It represents two tuples, one - with names, and one - with colors.

16. Unequal tuple sizes

If we have unequal tuple sizes within the list, the reverse operation will be restricted by the length of the "shortest" tuple. The items above this limit are not considered.

17. Relation to a dictionary

We saw that a zip object can be represented as a list of tuples each containing elements which indices coincide with the original Iterable objects. Recall that a dictionary object can be created from a list of tuples. Therefore, a zip object can be used to create a dictionary. Let's consider the following two lists. Now, let's initialize our dictionary using the result of the zip() initializer. Printing the dictionary gives us the following output.

18. Creating a DataFrame

Finally, we can use the newly created dictionary to build a DataFrame! We only have to insert the dictionary into the DataFrame initializer. The resulting DataFrame will look like this. So, we accomplished quite an amazing journey: we started from lists,

19. Creating a DataFrame

merged them within the zip object,

20. Creating a DataFrame

combined the result into a dictionary,

21. Creating a DataFrame

and created a DataFrame!

22. Let's practice!

Now you need to strengthen your knowledge about the use of zip objects. Therefore, let's practice!