The collections module in Python provides specialized container datatypes, such as:
Counter is a dictionary subclass designed for counting hashable objects. It maps elements to their counts.
Use Counter instead of a dictionary when you need:
from collections import Counter
c = Counter("hello world")
print(c) # Output: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
c1 = Counter("hello")
c2 = Counter("world")
print(c1 + c2) # Combined counts
print(c1 - c2) # Subtracted counts
namedtuple is a factory function in the collections module that creates tuple subclasses with named fields.
It is an immutable data structure that allows access to elements using field names instead of positional indices.
Use namedtuple instead of regular tuples when you need:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # Output: 10 20
p = p._replace(x=15) # Replace a value
print(p._asdict()) # Convert to dictionary: {'x': 15, 'y': 20}
deque (double-ended queue) is a list-like container with fast appends and pops from both ends. It is implemented as a doubly-linked list.
Use deque instead of a list when you need:
from collections import deque
d = deque([1, 2, 3])
d.append(4) # Append to the right
d.appendleft(0) # Append to the left
print(d) # Output: deque([0, 1, 2, 3, 4])
d.pop() # Remove from the right
d.popleft() # Remove from the left
print(d) # Output: deque([1, 2, 3])
d = deque([1, 2, 3, 4])
d.rotate(1) # Rotate to the right
print(d) # Output: deque([4, 1, 2, 3])
defaultdict is a dictionary subclass that provides a default value for non-existent keys.
Use defaultdict instead of a dictionary when you need:
from collections import defaultdict
d = defaultdict(int) # Default value of 0
d['a'] += 1
print(d) # Output: defaultdict(<class 'int'>, {'a': 1})
d = defaultdict(list)
d['a'].append(1)
print(d) # Output: defaultdict(<class 'list'>, {'a': [1]})