The Python `collections` module provides specialized container datatypes, beyond the common `list`, `tuple`, `dict`, and `set`. This enhances the possibilities for storing and managing data in your programs.
from collections import namedtuple
# Create a namedtuple
Point = namedtuple('Point', ['x', 'y'])
# Create instances
p1 = Point(10, 20)
p2 = Point(30, 40)
print(p1.x, p1.y) # Output: 10 20
print(p1) # Output: Point(x=10, y=20)
Creates a `Point` type that provides named fields (`x`, `y`) instead of just numeric indices. This improves readability.
from collections import Counter
words = "apple banana apple cherry banana apple".split()
word_counts = Counter(words)
print(word_counts) # Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})
Efficiently counts the occurrences of items in a sequence.
from collections import deque
queue = deque([1, 2, 3])
queue.append(4)
queue.appendleft(0)
print(queue) # Output: deque([0, 1, 2, 3, 4])
queue.pop()
print(queue) # Output: deque([0, 1, 2, 3])
Provides efficient appending and popping from both ends of a sequence. Useful for queues and stacks.
from collections import defaultdict
d = defaultdict(int)
d['a'] += 1
d['b'] += 2
d['a'] += 1
print(d['a']) # Output: 2
print(d['c']) # Output: 0
Creates a dictionary with a default value if a key doesn't exist. Here, a default value of 0 is assigned if a key is not found.
from collections import OrderedDict
d = OrderedDict()
d['a'] = 1
d['b'] = 2
d['c'] = 3
for key, value in d.items():
print(key, value)
Maintains the order of insertion of key-value pairs. Crucial for scenarios where order matters.