The built-in list, tuple, set, and dictionary cover most programs. The collections module supplies focused containers for counting, queues, defaults, and lightweight records; itertools supplies lazy building blocks for combining and slicing iterables.
After this lesson, you can choose Counter, deque, defaultdict, islice, chain, and groupby for their actual behavior and avoid materializing a sequence when the consumer only needs part of it.
Counter maps values to occurrence counts and supports total(), most_common(), and multiset-style arithmetic. Missing keys read as zero rather than raising KeyError.
from collections import Counter
tags = ["python", "sql", "python", "testing", "python", "sql"]
counts = Counter(tags)
print(counts.most_common(2))
[('python', 3), ('sql', 2)]
deque supports efficient appends and pops at both ends. Use append() with popleft() for a FIFO queue; a list pop(0) shifts remaining elements.
from collections import deque
tasks = deque(["validate", "save"])
tasks.append("notify")
print(tasks.popleft())
print(list(tasks))
validate
['save', 'notify']
defaultdict calls a factory when a missing key is accessed. Use it when automatic creation is the intended mutation; use dict.get() when a lookup should not change the mapping.
from collections import defaultdict
groups = defaultdict(list)
for level, lesson in [("beginner", "Lists"), ("advanced", "Async")]:
groups[level].append(lesson)
print(groups["beginner"])
['Lists']
itertools.chain joins iterables without concatenating them, while islice takes a bounded window without requiring sequence slicing. These tools preserve laziness when their inputs are lazy.
from itertools import chain, islice
combined = chain([1, 2], range(3, 100))
print(list(islice(combined, 5)))
[1, 2, 3, 4, 5]
groupby groups adjacent equal keys, not every matching key in the entire input. Sort by the same key first when records are not already grouped, and consume each group before advancing the outer iterator.
0 of 2 checked
Try this next
0 of 2 completed
Explore 500+ free tutorials across 20+ languages and frameworks.