The zip() function in Python is a powerful tool but often misunderstood by beginners. It's used to combine elements from multiple iterables (like lists, tuples) into tuples.
Quiz
Which one of these code samples correctly demonstrates the use of the zip() function in Python?
Sample 1
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")
Sample 2
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for i in range(len(names)):
print(f"{names[i]} is {ages[i]} years old.")
Post answer that you think is correct or just try it in the REPL!
Follow me to learn 🐍 Python in 5-minute a day fun quizzes!
Top comments (2)
Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.
For both samples
You are right. But the second example does not involve
zip
at all! It demonstrates one of the powers ofzip
! 😄