This tutorial brilliantly demystifies the `zip()` function by transforming abstract logic into intuitive visual patterns. It is a masterclass in educational efficiency that makes Python’s list pairing both accessible and memorable.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Python zip() Function - Visually Explained
Added:You're given a list of students and a separate list of their test scores.
Your job is to print a report containing both the students' names and their test scores.
But, here's the problem.
By default, a for loop can only iterate through one list at a time.
So, how can we print our report?
Pairing items from multiple lists at once is a very common need in programming.
So common that Python has a built-in way to do it. The zip function.
To use the function, we call it right in our loop header.
Into that call, we pass the two lists we want to pair.
Students and their scores.
Since we don't yet know what zip will generate on each iteration, let's rename our loop variable X.
To see what X is, we'll print it.
Running the code, we see that zip produces pairs of students and grades.
These pairs are called tuples.
If you want to learn more about tuples, including how they compare to lists and sets, there's a whole video on it linked in the description.
Okay, we've made good progress.
Using zip allowed our loop to walk over two lists at the same time.
But, to format our report with a dash in between the name and the grade, we need them both as separate variables.
We could pull them out of the tuple by indexing it ourselves, with pair zero for the name and pair one for the score.
Running the code, it works.
But, indexing manually makes our loop a bit harder to read.
Someone taking a fresh look at this code would have to refer back to the loop header to see that X0 contains a student name and not a grade.
Only there can we see that students are first in zip. So, X0 must represent a student.
Not very transparent.
To improve the readability of our loop, Python lets us assign the tuple's values to separate variables directly in the header.
To split the tuple, we replace X with two variables, student and score, separated by a comma.
With this header, the zip function still produces a tuple.
For example, one with Alice and 85.
But now, that tuple is pulled apart into two separate variables.
In Python, splitting a tuple into separate variables is called tuple unpacking.
Once they've been assigned in the header, we can use these two variables in the loop body just like any other variable.
Running the code, we get each student's name printed with their score.
Nice. Unpacking gave the name and score their own variables. So, we can finally print our report.
Now, a report with names and grades is useful for a teacher, but students want to know their actual letter grade.
Can we use zip to pair a third list containing letter grades with the other two?
The answer is yes.
The zip function can pair any number of lists.
To add the third list, we simply pass it as another argument in the zip function.
Now, each tuple generated by zip will hold three values.
To capture this third value, we add a third loop variable.
Finally, we can output this variable in the loop body.
Running the code, we get each student's name, score, and a letter grade, all matched up.
This pairing works perfectly when all of the lists have the same number of elements.
But sometimes they don't.
Suppose Bob missed the test. So, students still has three elements, but scores and grades only have two.
Running the code, we only get two pairs.
That's because zip stops creating pairs the moment it reaches the end of the shortest list.
Here, both scores and grades.
Looking a bit closer, Bob was assigned a score even though he didn't take the test.
This happened because zip matches items purely by position.
It has no idea which ones truly belong together.
Worst of all, Python didn't raise an error or give us any warning about either of these issues.
So, the lesson here is when you're zipping several lists that all have to stay in sync, you need to be careful.
If you can change how the data is stored, it would probably be safer to use a dictionary here.
If you want to learn more about dictionaries, there's a whole video on them linked in the description.
But, if you're just combining a few lists and you know they're the same length, zip is a great choice.
Awesome. By now, you should feel comfortable using zip to pair lists together.
But, it's not the only way.
Especially in older code, you'll see the same job done by looping over the indexes with range and len.
Then, to access the list items, the code has to use manual indexing.
This does work and it produces the same output.
But, as we discussed earlier, using manual indexing makes the loop harder to read and more error-prone.
So, if you find code like this, reach for zip instead.
It's the clearest and safest way to loop through several lists at once.
If you'd like to practice what you've learned, check out the notebook we created for this video linked in the description.
We're working on lots more Python explainer videos like this one, so be sure to subscribe so that you don't miss out. If you have any questions or topics that you'd like to learn about, let us know in the comments below. We'd love to hear from you. Thanks for watching.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23