This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python enumerate and zip, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does enumerate produce on each pass?
Answer: An index and an item
Why: enumerate yields a pair containing the position and the value.
What does this code print?
print(list(enumerate(["a", "b"], start=1)))Answer: [(1, 'a'), (2, 'b')]
Why: start=1 begins the numbering at one, and list() shows each pair.
What does zip do when the lists have different lengths?
Answer: Stops at the shortest list
Why: zip stops as soon as one input is exhausted.
Which loop prints 1 Ada and 2 Grace?
Answer: for n, name in enumerate(names, start=1):
Why: enumerate takes the iterable first and start as a keyword argument.
Complete the loop so each item gets an index.
items = ["a", "b"]
for i, item in ____(items):
print(i, item)Answer: enumerate
Why: enumerate yields the index and item together.
Complete the argument that starts numbering at one.
for n, item in enumerate(["a", "b"], ____=1):
print(n, item)Answer: start
Why: The keyword argument is start.
Complete the function that pairs two lists.
names = ["Ada", "Grace"]
scores = [92, 88]
pairs = list(____(names, scores))Answer: zip
Why: zip walks two iterables in parallel.
Complete the call that produces (0, 'a') first.
print(next(____(["a"])))Answer: enumerate
Why: next() asks enumerate for its first pair.
What does the program output?
print(list(enumerate(["x", "y"])))Answer: [(0, 'x'), (1, 'y')]
Why: The default start is zero, and list() shows the pairs.
What appears on the screen?
print(list(zip([1, 2], ["a"])))Answer: [(1, 'a')]
Why: zip stops when the shorter input runs out, so only one pair remains.
What is printed?
print([i for i, _ in enumerate("abc", start=2)])Answer: [2, 3, 4]
Why: The indexes are 2, 3, and 4 even though the characters are not used.
What is the output?
print(dict(zip(["a", "b"], [1, 2])))Answer: {'a': 1, 'b': 2}
Why: zip creates pairs, and dict turns each pair into a key and value.
Try the questions first
Open the practice page for Python enumerate and zip and answer the same 12 questions with instant feedback.