Lesson 4 · Python standard library deep dive
Python collections Explained: Counter, deque & defaultdict | Standard Library #4
Collections, specialized container types that solve problems the built-in list, dict, and tuple genuinely don't cover well. Counter, defaultdict, deque,…
- CoursePython standard library deep dive
- Lesson4 of 24
- Video17 min
- FormatJupyter notebook · 14 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Standard Library Deep-Dive, Video 4: collections#
- Video four of the twenty-five-part series: collections, specialized container types that solve problems the built-in list, dict, and tuple genuinely don't cover well.
- Counter, defaultdict, deque, namedtuple, OrderedDict, and ChainMap.
- Let's get into it.
Part 1: What collections Offers#
from collections import Counter, defaultdict, deque, namedtuple, OrderedDict, ChainMap
print('all six imported successfully')
Part 2: Counter#
words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
counts = Counter(words)
print(counts)
print(counts['apple'])
print(counts['durian'])
print(isinstance(counts, dict))
counts = Counter('mississippi')
print(counts)
print(counts.most_common())
print(counts.most_common(2))
print(list(counts.elements()))
counts.update('extra')
print(counts)
a = Counter(['x', 'x', 'y'])
b = Counter(['x', 'y', 'y', 'z'])
print(a + b)
print(a - b)
print(a & b)
print(a | b)
Part 3: defaultdict#
groups = defaultdict(list)
groups['fruits'].append('apple')
groups['fruits'].append('banana')
groups['vegetables'].append('carrot')
print(dict(groups))
regular = {}
try:
regular['fruits'].append('apple')
except KeyError as e:
print(f'Caught: {e}')
word_lengths = defaultdict(int)
for word in ['cat', 'dog', 'elephant', 'ox', 'cat']:
word_lengths[word] += 1
print(dict(word_lengths))
tree = lambda: defaultdict(tree)
nested = tree()
nested['a']['b']['c'] = 'deep value'
print(nested['a']['b']['c'])
Part 4: deque#
d = deque([1, 2, 3])
d.append(4)
d.appendleft(0)
print(d)
print(d.pop())
print(d.popleft())
print(d)
recent = deque(maxlen=3)
for i in range(6):
recent.append(i)
print(list(recent))
d = deque([1, 2, 3, 4, 5])
d.rotate(2)
print(d)
d.rotate(-2)
print(d)
Part 5: namedtuple#
Point = namedtuple('Point', ['x', 'y', 'z'])
p = Point(1, 2, 3)
print(p)
print(p._fields)
print(p._asdict())
updated = p._replace(z=99)
print(updated)
print(p)
values = [10, 20, 30]
from_list = Point._make(values)
print(from_list)
Part 6: OrderedDict#
od = OrderedDict()
od['first'] = 1
od['second'] = 2
od['third'] = 3
print(od)
od.move_to_end('first')
print(od)
od.move_to_end('third', last=False)
print(od)
od1 = OrderedDict([('a', 1), ('b', 2)])
od2 = OrderedDict([('b', 2), ('a', 1)])
print(od1 == od2)
d1 = {'a': 1, 'b': 2}
d2 = {'b': 2, 'a': 1}
print(d1 == d2)
Part 7: ChainMap#
defaults = {'theme': 'light', 'font_size': 12, 'language': 'en'}
user_settings = {'font_size': 16}
combined = ChainMap(user_settings, defaults)
print(combined['font_size'])
print(combined['theme'])
print(dict(combined))
user_settings['theme'] = 'dark'
print(combined['theme'])
Part 8: Common Patterns#
text = 'the quick brown fox jumps over the lazy dog the fox runs'
word_counts = Counter(text.split())
print(word_counts.most_common(3))
def moving_average(values, window_size):
window = deque(maxlen=window_size)
averages = []
for value in values:
window.append(value)
averages.append(sum(window) / len(window))
return averages
data = [10, 20, 30, 40, 50, 60]
print(moving_average(data, 3))
Wrap-Up: What You Learned#
- Counter: fast tallying, most_common, elements, and set-like arithmetic between counters.
- defaultdict: automatic default values from a factory function, eliminating manual key checks.
- deque: fast operations at both ends, maxlen for bounded history, and rotate.
- namedtuple in depth: _fields, _asdict, _replace, _make.
- OrderedDict: move_to_end and order-sensitive equality, still relevant despite dict's own ordering.
- ChainMap: a live, layered view across multiple dictionaries with no copying.
- Two real patterns: word-frequency counting and a deque-based moving average.
- That wraps up collections. Next up: itertools, for efficient iteration and combinatorics.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does this print?
from collections import Counter print(Counter('mississippi').most_common(2))
i and s each appear 4 times; ties keep the order they were first seen.
What does this print?
from collections import Counter a = Counter(['x', 'x', 'y']) b = Counter(['x', 'y', 'y', 'z']) print(a - b)
Counter subtraction keeps only positive counts: x is 2-1=1, while y and z drop to zero or below and disappear.
What does this print?
from collections import defaultdict d = defaultdict(list) d['a'].append(1) d['b'] print(len(d))
Just looking up a missing key creates it with the default factory, so 'b' now exists with an empty list.
What does this print?
from collections import deque recent = deque(maxlen=3) for i in range(6): recent.append(i) print(list(recent))
With maxlen set, adding to the right pushes the oldest items out the left.
What does this print?
from collections import ChainMap user = {'font': 16} defaults = {'font': 12, 'theme': 'light'} cm = ChainMap(user, defaults) user['theme'] = 'dark' print(cm['theme'], cm['font'])
ChainMap is a live view that searches its maps in order, so the later change to user shows up and user wins.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Count the letters in 'abracadabra' with Counter and print the top 3 most common.
💡 Hint
Pass the string to Counter and call most_common(3).
✅ Show solution
from collections import Counter counts = Counter("abracadabra") print(counts.most_common(3))
Group the words ['apple', 'fig', 'kiwi', 'pear', 'plum', 'banana'] by length using defaultdict(list) and print the result as a normal dict.
💡 Hint
Append each word to groups[len(word)].
✅ Show solution
from collections import defaultdict groups = defaultdict(list) for word in ["apple", "fig", "kiwi", "pear", "plum", "banana"]: groups[len(word)].append(word) # list is created automatically print(dict(groups)) assert groups[4] == ["kiwi", "pear", "plum"]
Write moving_average(values, window_size) using a deque with maxlen, returning the average after each new value. Then define a namedtuple Reading('sensor', 'value') and use _replace to correct one reading.
💡 Hint
Append each value to deque(maxlen=window_size) and compute sum(window) / len(window).
✅ Show solution
from collections import deque, namedtuple def moving_average(values, window_size): window = deque(maxlen=window_size) averages = [] for v in values: window.append(v) # old values fall off automatically averages.append(sum(window) / len(window)) return averages result = moving_average([10, 20, 30, 40], 2) print(result) assert result == [10.0, 15.0, 25.0, 35.0] Reading = namedtuple("Reading", ["sensor", "value"]) r = Reading("temp", 20) fixed = r._replace(value=21) print(r, fixed) assert fixed.value == 21 and r.value == 20
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



