Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
defaultdict avoids key-existence checks when grouping, Counter counts items and finds the most common ones, deque gives fast appends and pops from both ends, and namedtuple creates lightweight records.
defaultdict(list) lets you write groups[key].append(item) without checking whether the key exists, which simplifies grouping code. Counter(words).most_common(10) counts and ranks items in one line and supports arithmetic between counters. deque supports O(1) appends and pops at both ends, unlike list.pop(0), which is O(n), so it is the right choice for queues, sliding windows and BFS, and deque(maxlen=n) keeps only the last n items. OrderedDict is still useful for move_to_end in LRU-style logic, and ChainMap layers several dictionaries, for example for configuration defaults.