How To Use the Python Zip Function for Multiple Iteration
The Python zip function is an important tool that makes it easy to group data from multiple data structures. It is commonly used to loops over multiple data structures at once, without having to create nested loops.
In this tutorial, I will show you how to use the Python zip function to perform multiple iterations over parallel data structures.
Table of Contents
You can skip to a specific section of this tutorial below:
The Python zip function is used to merge multiple objects, called iterables. More specifically, it creates a new object whose elements are tuples. Within a specific tuple, the elements of the iterables are held.
A simple example is helpful to understand the Python zip function. Imagine that you have two Python tuples of names, like this:
There are many more specifics to the zip function, but that’s its high-level overview.
If you are interested in the technical details, here is the official definition of the Python zip function from the official Python documentation website:
Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
More information can be found in the official documentation on the Python zip function here.
For anyone who wants to see the source code of the Python zip function, the language’s maintainers have provided it on the documentation website. Here is the source code for the Python zip function:
defzip(*iterables):# zip('ABCD', 'xy') --> Ax By
We will explore more of the characteristics and functionality of the Python zip function throughout the rest of this tutorial.
How to Use the Python zip Function With Different Data Structures
In the first example, how the Python zip function can combine two lists into one zip object whose elements are each tuples of length 2.
It is important to understand that the Python zip function is actually capable of working with many different data structures. We will explore how to use Python zip with different data structures in this section.
How to Use Python zip With Tuples
You can pass in two tuples into the Python zip function exactly as we did with lists.
How to Use the Python zip Function With More Than Two Arguments
Although the examples we have seen in this tutorial have only included two iterables, the Python zip function is theoretically capable of accepting unlimited arguments. This section will demonstrate this capability.
To start, let’s define three variables that we’d like to create a zip object with. The variables are below:
canada_cities=['Toronto','Montreal','Fredericton']us_cities=['New York City','San Francisco','Houston']europe_cities=['London','Paris','Munich']
We can zip these three objects together by passing them into the Python zip function like this:
How the Python zip Function Handles Arguments of Different Length
So far in this tutorial, we have only applied the Python zip functions to data structures of the same length. The zip function can accept arguments with different lengths. I will demonstrate this capability in this section.
Earlier in this tutorial, I embedded the explanation of the Python zip function from the official documentation website.
The description included the following sentence: ‘The iterator stops when the shortest input iterable is exhausted.’
This means that the length of the output of the Python zip function will be equal to the length of its smallest argument.
Let’s consider an example to help solidify this.
Specifically, let’s examine a situation where you have a group of men and a group of women, and you want to pair them together for duo dance lessons. Here’s how the Python zip function could help with this:
How To Loop Over Multiple Objects in Python Using Python zip
One of the main purposes of the Python zip function is the ability to iterate over multiple objects simultaneously. The syntax for this is very Pythonic and easy to remember, which I why I wanted to conclude this tutorial with this topic.
Looping with Python zip is best understood through an example. Imagine that you have a list of students and lists of grades, like this:
In this tutorial, you learned how to use the Python zip function to pair elements from different data structures and iterate through them using for loops. If you’re interested in learning more Python concepts, check out my courses Python Fundamentals and Advanced Python for Finance.