Answer key · 12 questions

Python collections Exercises With Answers

All 12 questions for Python collections in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python collections, 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.

01 · Multiple choice

What does Counter(votes) return?

  1. A tuple of values
  2. A set of unique values
  3. A sorted list of values
  4. A mapping of values to countscorrect

Answer: A mapping of values to counts

Why: Counter is a dictionary subclass whose values are occurrence counts.

02 · Multiple choice

What does this code print?

from collections import Counter
print(Counter("aba")["a"])
  1. 0
  2. 1
  3. 2correct
  4. 3

Answer: 2

Why: The string has two a characters, and a missing key would return zero.

03 · Multiple choice

What does defaultdict(list) do for a missing key?

  1. Raises KeyError
  2. Returns None
  3. Creates an empty listcorrect
  4. Deletes the key

Answer: Creates an empty list

Why: The list factory supplies the default value and inserts the key.

04 · Multiple choice

Which deque call removes the leftmost item?

  1. removeleft()
  2. popleft()correct
  3. shift()
  4. pop()

Answer: popleft()

Why: popleft removes and returns the item at the left end of the deque.

05 · Fill in the blank

Complete the import for the counting container.

from collections import ____
print(Counter(["a", "a"]))

Answer: Counter

Why: Counter is the counting dictionary in collections.

06 · Fill in the blank

Complete the method that returns the most common entries.

from collections import Counter
counts = Counter(["a", "b", "a"])
print(counts.____(1))

Answer: most_common

Why: most_common returns entries ordered by descending count.

07 · Fill in the blank

Complete the factory for a list-valued defaultdict.

from collections import defaultdict
groups = defaultdict(____)

Answer: list

Why: Passing list creates an empty list for a missing key.

08 · Fill in the blank

Complete the call that removes the oldest queue item.

from collections import deque
queue = deque([1, 2])
queue.____()

Answer: popleft

Why: popleft removes from the left end of a deque.

09 · Predict output

What does the program output?

from collections import Counter
print(Counter("abca")["a"])

Answer: 2

Why: The character a appears twice.

10 · Predict output

What appears on the screen?

from collections import Counter
print(Counter([1, 1, 2]).most_common(1))

Answer: [(1, 2)]

Why: The most common entry is 1 with a count of 2.

11 · Predict output

What is printed?

from collections import defaultdict
d = defaultdict(list)
d["x"].append(1)
print(dict(d))

Answer: {'x': [1]}

Why: The missing key receives an empty list, which then gets one item.

12 · Predict output

What is the output?

from collections import deque
q = deque([1, 2, 3])
q.popleft()
print(list(q))

Answer: [2, 3]

Why: popleft removes the first item, leaving two and three.

Try the questions first

Open the practice page for Python collections and answer the same 12 questions with instant feedback.

Practice this lesson