Challenge - 5 Problems
Zip Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of zip with lists of different lengths
What is the output of this code?
list1 = [1, 2, 3, 4] list2 = ['a', 'b'] result = list(zip(list1, list2)) print(result)
Python
list1 = [1, 2, 3, 4] list2 = ['a', 'b'] result = list(zip(list1, list2)) print(result)
Attempts:
2 left
๐ก Hint
zip stops when the shortest input is exhausted.
โ Incorrect
The zip function pairs elements from each list until one list runs out. Here, list2 has only 2 elements, so zip stops after pairing two elements.
โ Predict Output
intermediateUsing zip with unpacking operator
What is the output of this code?
pairs = [(1, 'x'), (2, 'y'), (3, 'z')] numbers, letters = zip(*pairs) print(numbers) print(letters)
Python
pairs = [(1, 'x'), (2, 'y'), (3, 'z')] numbers, letters = zip(*pairs) print(numbers) print(letters)
Attempts:
2 left
๐ก Hint
The * operator unpacks the list of tuples into separate arguments for zip.
โ Incorrect
Using *pairs unpacks the list into separate tuples, so zip groups first elements together and second elements together, returning two tuples.
โ Predict Output
advancedOutput of zip with generator expressions
What is the output of this code?
gen1 = (x*x for x in range(3)) gen2 = (x+1 for x in range(5)) result = list(zip(gen1, gen2)) print(result)
Python
gen1 = (x*x for x in range(3)) gen2 = (x+1 for x in range(5)) result = list(zip(gen1, gen2)) print(result)
Attempts:
2 left
๐ก Hint
zip stops when the shortest generator is exhausted.
โ Incorrect
gen1 produces 3 values (0,1,4), gen2 produces 5 values (1,2,3,4,5). zip stops after 3 pairs because gen1 ends.
โ Predict Output
advancedResult of zipping dictionaries
What is the output of this code?
dict1 = {'a': 1, 'b': 2}
dict2 = {'x': 10, 'y': 20, 'z': 30}
result = list(zip(dict1, dict2))
print(result)Python
dict1 = {'a': 1, 'b': 2}
dict2 = {'x': 10, 'y': 20, 'z': 30}
result = list(zip(dict1, dict2))
print(result)Attempts:
2 left
๐ก Hint
Iterating a dictionary yields its keys.
โ Incorrect
When you zip dictionaries, it zips their keys. dict1 has keys 'a' and 'b', dict2 has keys 'x', 'y', 'z'. zip stops after shortest dict keys.
๐ง Conceptual
expertBehavior of zip with infinite iterator
Consider this code:
What is the output?
import itertools infinite = itertools.count(1) finite = [10, 20, 30] result = list(zip(infinite, finite)) print(result)
What is the output?
Attempts:
2 left
๐ก Hint
zip stops when the shortest input is exhausted, even if the other is infinite.
โ Incorrect
zip stops after the finite list ends. The infinite iterator produces values but zip stops after 3 pairs.
