Working With Iterables

Mastering the zip() Function in Python

In this lesson, you will learn how to use Python's zip() function to pair elements from multiple lists or tuples, iterate through zipped results, and apply practical techniques for combining and processing data in parallel.


In this lesson, you'll master the zip() function for pairing items from multiple lists or tuples.

Let's start by combining two lists of equal length with zip().

Python
Output

zip() pairs items by their position, creating tuples of each matching element.

Here's the syntax for using zip():

Python

You can also use zip() with tuples.

Python
Output

What will be the output?

Python

What if your lists are different lengths? Let's see what happens.

zip() stops at the shortest list when lengths differ.

Python
Output

What will be the output?

Python

Here's how you might store zipped pairs in a variable:

Python

You can loop through a zipped result to process pairs.

Python
Output

You can also unpack values from each zipped tuple directly in the loop.

Python

Let's use unpacking in a real example.

Python
Output

What will be the output?

Python

zip() can combine more than two sequences at once. Let's see how.

Try zipping three liststhe result contains tuples with three elements.

Python
Output

What will be the output?

Python

zip() is useful for building new lists from pairs. Let's try it!

Here's how you can create a list of strings by combining zipped pairs in a loop.

Python
Output

What will be the output?

Python

Remember, zip() always creates tuples—never lists or dictionaries.

Python

Let's see how zip() can quickly pair up values for reporting.

Python
Output

What will be the output?

Python

What will be the output?

Python