Python on Speed ⚡: 33. zip() Function in Python

Опубликовано: 24 Апрель 2026
на канале: Bodan Labs
1,385
20

Learn how to use Python zip() to pair lists and clean up your loops in seconds. In this quick tutorial, you’ll see how to iterate multiple lists in Python, create list-of-tuples, and even unzip data. We’ll also cover itertools.zip_longest for uneven lists and show when to use dict(zip(...)) for instant lookups. Perfect for Python beginners and anyone who writes cleaner, faster code.

💡 What you’ll learn
• What zip() does and why it’s cleaner than index-based loops
• Pairing lists: for a, b in zip(listA, listB)
• See the pairs directly: list(zip(names, scores))
• Unzip: names, scores = zip(*pairs)
• Uneven lists: from itertools import zip_longest
• Quick mapping: dict(zip(keys, values))

🧪 Code from the Short

names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]

for name, score in zip(names, scores):
print(name, score)

print(list(zip(names, scores))) # [('Alice',85), ('Bob',92), ('Charlie',78)]

Unzip
pairs = list(zip(names, scores))
unzipped_names, unzipped_scores = zip(*pairs)

Handle uneven lists
from itertools import zip_longest
long_pairs = list(zip_longest(["A","B"], [1,2,3], fillvalue=None))

Build a dict fast
grades = dict(zip(names, scores))


🚀 Next steps
Comment “ZIP” for a cheat sheet, and subscribe for the next Python on Speed tip!

#Python #Shorts #LearnPython #Coding #Programming #PyTricks