Remember
zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.
zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))Top-level scope
range(0, 3)Object 2 · list['a', 'b']zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))
|
v
DISPLAYED RESULT
[(0, 'a'), (1, 'b')]
FINAL BINDINGS
numbers --> object 1 (range)
letters --> object 2 (list)
object 1: range(0, 3)
object 2: ['a', 'b']Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))Top-level scope
range(0, 3)Object 2 · list['a', 'b']The example has finished. Compare the final output with the values and references above.
[(0, 'a'), (1, 'b')]
What do you expect this program to print?
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))zip() FunctionThe zip() function in Python combines two or more iterables (like lists or tuples) element by element into pairs or groups. It’s extremely useful when you want to loop through multiple sequences at once.
zip(iterable1, iterable2, ...)
iterable1, iterable2, ... – Two or more iterables (lists, tuples, strings, etc.)names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]
zipped = zip(names, scores)
print(list(zipped))
[('Alice', 85), ('Bob', 90), ('Charlie', 95)]
a = [1, 2, 3]
b = ['a', 'b', 'c']
c = [True, False, True]
print(list(zip(a, b, c)))
[(1, 'a', True), (2, 'b', False), (3, 'c', True)]
When iterables passed to zip() are not of the same length, it stops at the shortest one.
x = [1, 2]
y = ['a', 'b', 'c']
print(list(zip(x, y)))
[(1, 'a'), (2, 'b')]
keys = ['id', 'name', 'age']
values = [101, 'Alice', 25]
person = dict(zip(keys, values))
print(person)
{'id': 101, 'name': 'Alice', 'age': 25}
for name, score in zip(names, scores):
print(f"{name} scored {score}")
Alice scored 85
Bob scored 90
Charlie scored 95
You can reverse a zipped object using the unpacking operator *:
pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
a, b = zip(*pairs)
print(a)
print(b)
(1, 2, 3)
('a', 'b', 'c')
print(zip(...)) will just show the object type.zip() to handle different lengths – it doesn't pad, it truncates.zip() is frequently used in data pairing, matrix manipulation, and transforming data between structures. Be ready to combine or unzip data structures in questions.
zip() combines multiple iterables into tuples.Given two lists of student names and their grades, print each student with their grade using zip().
students = ["Ravi", "Meena", "Arjun"]
grades = ["A", "B", "A+"]
for student, grade in zip(students, grades):
print(f"{student} got grade {grade}")
Connect what you noticed in the example with the idea below.
zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.
zip() always fills missing items with None.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.