How to Use the Python zip() Function
Have you ever written a piece of Python code where you need to aggregate elements from multiple variables where the first entry in one container is paired with the first entry in the second container? This is when you call the Python zip() function.
To illustrate this, let’s use a row of numbers and a row of letters like this:
|
1 2 |
(1, 2, 3, 4, 5) (a, b, c, d, e) |
Using the zip() function, the iteration will look like this:
|
1 |
((1, a), (2, b), (3, c), (4, d), (5, e)) |
Another example, using first and last names:
|
1 2 |
(Aaron, Anton, Jean, Camille, Clara) (Kennedy, Frank, Barber, St. Jude, Beach) |
The above tuples iterate to:
|
1 |
((Aaron, Kennedy), (Anton, Frank), (Jean, Barber), (Camille, St. Jude), (Clara, Beach)) |
Of course, for something like this to make better sense, you might place the last names in the first container and the first names in the second container, so the output would be:
|
1 |
((Kennedy, Aaron), (Frank, Anton), (Barber, Jean), (St. Jude, Camille), (Beach, Clara)) |
Fun times.
The zip() function can be used with tuples, lists, or any other type of Python iterable and makes it easy to combine two or more containers into a single container. In the end, the resultant iterable will contain tuples (a collection that is ordered and unchangeable).
I want to show you how to use the Python zip() function in a couple of different ways. The first will be a simple demonstration as to how zip() works and the second will incorporate some more complicated ideas. In the end, they both simply iterate through a container and create a tuple that can be either printed or used within your program.
What You’ll Need
To follow along, you’ll need Python 3 installed on your computer. It doesn’t matter what operating system you are using, so long as it supports Python. I’ll demonstrate on Ubuntu Budgie, but you do it with whatever OS you prefer.
Our first example will take two lists of names (one first and one last) and combine them together into a zip object of tuples, such that we’ll see last name and first name separated by a comma.
The zip() function is included in the basic Python library, so we don’t have to import anything and can dive right into our script.
The first line will define our list of last names and will look like this:
|
1 |
last = ["Kennedy", "Frank", "Barber", "St. Jude", "Beach"] |
Our second line defines first names as b and looks like this:
|
1 |
first = ["Aaron", "Anton", "Jean", "Camille", "Clara"] |
Our list line will define names with the zip() function, like so:
|
1 |
names = zip(last, first) |
Finally, we use the print() and tuple() functions to print the aggregated list like so:
|
1 |
print(tuple(names)) |
Create the new file with the command:
|
1 |
nano names.py |
Paste the following content into the new file:
|
1 2 |
last = ["Kennedy", "Frank", "Barber", "St. Jude", "Beach"] first = ["Aaron", "Anton", "Jean", "Camille", "Clara"] |
|
1 2 3 |
names = zip(last, first) print(tuple(names)) |
Save and close the file.
Run the program like so:
|
1 |
python3 names.py |
As expected, the output will be:
|
1 |
(('Kennedy', 'Aaron'), ('Frank', 'Anton'), ('Barber', 'Jean'), ('St. Jude', 'Camille'), ('Beach', 'Clara')) |
Let’s make this a bit more challenging, shall we?
Our Second Example
This time around, what we’re going to do is define a list of first names, define a list of last names, and use the zip() function to iterate a random order of the combined tuples. This can be a handy way to assign an order for people to follow.
This script will use the zip(), print(), and set() functions. You already know what the zip() and print() functions do. The set() function takes a pair of lists and combines them into a seemingly random order. This isn’t true randomness, but it works for our purposes.
We’ll define our first names as names with:
|
1 |
fnames = ["Aaron", "Anton", "Jean", "Camille", "Clara"] |
Next, we’ll define last names like so:
|
1 |
lnames = ["Kennedy", "Frank", "Barber", "St. Jude", "Beach"] |
We now define our zip() function (using our lists fnames and lnames) as mapped like so:
|
1 |
mapped = zip(fnames, lnames) |
Finally, we combine the print() and set() functions like this:
|
1 |
print(set(mapped)) |
Create a new file with the command:
|
1 |
nano random.py |
In that file, paste the following:
|
1 2 3 4 5 6 |
fnames = ["Aaron", "Anton", "Jean", "Camille", "Clara"] lnames = ["Kennedy", "Frank", "Barber", "St. Jude", "Beach"] mapped = zip(fnames, lnames) print(set(mapped)) |
Save and close the file.
Run our new Python app like this:
|
1 |
python3 random.py |
The output might look like this:
|
1 |
{('Anton', 'Frank'), ('Clara', 'Beach'), ('Aaron', 'Kennedy'), ('Jean', 'Barber'), ('Camille', 'St. Jude')} |
Run it again and the output might look like this:
|
1 |
{('Camille', 'St. Jude'), ('Anton', 'Frank'), ('Aaron', 'Kennedy'), ('Jean', 'Barber'), ('Clara', 'Beach')} |
Yet again, the output might look like:
|
1 |
{('Aaron', 'Kennedy'), ('Anton', 'Frank'), ('Jean', 'Barber'), ('Camille', 'St. Jude'), ('Clara', 'Beach')} |
What we’ve created is a simple app to generate a (seemingly) random order of names, thanks to the Python zip() function.
You could also use zip() with more than two iterables. Let’s throw middle names into the mix with the above application, with might look something like this:
|
1 2 3 4 5 6 7 |
fnames = ["Aaron", "Anton", "Jean", "Camille", "Clara"] mnames = ["Lee", "James", "Isobelle", "Jessica", "Summer"] lnames = ["Kennedy", "Frank", "Barber", "St. Jude", "Beach"] mapped = zip(fnames, mnames, lnames) print(set(mapped)) |
And that’s the basics of using the Python zip() function.