Built-ins & Standard Library · Question 8 · Medium

A Counter is built from a list of drink orders and is then asked about a drink nobody ordered. What is printed?

from collections import Counter

orders = ["tea", "coffee", "tea", "milk", "tea", "coffee"]
c = Counter(orders)
print(c.most_common(1)[0], c["juice"])
  1. ('tea', 3) None
  2. tea 3 0
  3. ('tea', 3) 0
  4. KeyError
Answer and explanation

Answer: C. ('tea', 3) 0

Explanation: most_common(1) returns a list with one (item, count) tuple, here ('tea', 3). A Counter returns 0 for a missing key instead of raising KeyError.

Previous: enumerate is given start=1 while looping over two names. What does the loop… · Next: Words are grouped by their first letter using defaultdict(list). What does the…

Practise built-ins & standard library

Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.

More built-ins & standard library questions

More python practice

Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge