Python

The collections Module

25 flashcards · answers and spaced-repetition review in the KnowCard app

When should you prefer dict.setdefault(k, []).append(x) over a defaultdict(list)?
You need to look up scores by team name when the data is split across several separate dicts (group_a, group_g, ...). You don't want to merge them into a new dict. Which collections type fits?
What does this print? ``` from collections import ChainMap a = {"x": 1} b = {"x": 2, "y": 9} cm = ChainMap(a, b) print(cm["x"], cm["y"]) ```
With cm = ChainMap(a, b), you do cm["x"] = 100. Which underlying dict gets modified?
You want to count how often each character appears in a string, with the least code. Which collections type, and how in one line?
What does this print? ``` from collections import Counter c = Counter("aabbbc") print(c["z"]) print("z" in c) ```
What does this print? ``` from collections import Counter c = Counter("abracadabra") print(c.most_common(2)) ```
What's the difference between Counter.update(other) and Counter.subtract(other) when combining two counters?
What does this print? ``` from collections import Counter a = Counter(x=3, y=1) b = Counter(x=1, y=2) print(a - b) ```
You're grouping words by length into lists. With a plain dict you wrote if k in d: d[k].append(w) else: d[k] = [w]. What collections type removes that branch, and how?
What does this print? ``` from collections import defaultdict d = defaultdict(int) x = d["missing"] print(x, len(d)) ```
What does this print? ``` from collections import defaultdict d = defaultdict(list) if d["k"]: print("yes") print(dict(d)) ```
Building a list of 10000 items by inserting each at the front (l.insert(0, x)) is far slower than appending to the end. Why, and what collections type fixes it?
What does this print? ``` from collections import deque d = deque([1, 2, 3]) d.extendleft([4, 5]) print(d) ```
What does this print? ``` from collections import deque d = deque([1, 2, 3], maxlen=3) d.append(4) print(d) ```
deque supports indexing like d[2]. Why is reaching into the middle of a large deque a performance trap compared to a list?
You return a coordinate as a 3-tuple and callers keep writing point[0]/point[1]. You want readable point.x access but keep tuple behavior. Which collections type?
What does this print? ``` from collections import namedtuple P = namedtuple("P", "x y") p = P(1, 2) p.x = 10 print(p) ```
How do you create a modified copy of a namedtuple and how do you convert one to a dict? (and why are these methods named with an underscore?)
namedtuple has a field_names parameter and an optional rename. What does rename=True do, and when do you need it?
Given a namedtuple type, how do you discover its field names at runtime (e.g. to iterate over them or serialize generically)?
A namedtuple gives named access to a structured record. What structural limitations make a dataclass a better fit for modelling, say, an address?
What does this print? from collections import Counter c = Counter(a=3, b=1) print(sorted(c.elements()))
What does this print? from collections import deque d = deque([1, 2, 3, 4, 5]) d.rotate(2) print(d)
When calling namedtuple("P", field_names), what forms may field_names take? Are these three equivalent? namedtuple("P", ["x", "y"]) namedtuple("P", "x y") namedtuple("P", "x,y")

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
The collections Module (Python) · KnowCard