The collections
module in Python provides specialized container data types that extend the functionality of standard lists, tuples, dictionaries, and sets. These specialized containers offer better performance, readability, and flexibility for specific use cases.
Why Use collections
?
Optimized for performance
Easier and more readable syntax
Useful for handling complex data structures
1. Common Data Structures in collections
Module
The collections
module includes several useful classes:
- Counter → Counts occurrences of elements
- defaultdict → Provides default values for missing keys
- OrderedDict → Maintains the order of dictionary keys
- deque → Fast list-like structure optimized for appends/pops
- namedtuple → Immutable, readable tuples with named fields
- ChainMap → Combines multiple dictionaries into one
2. Counter
– Counting Elements
Counter
is a dictionary subclass that counts hashable objects.
Example: Counting Words in a String
from collections import Counter
text = "apple banana apple orange banana apple"
word_count = Counter(text.split())
print(word_count)
Output:
Counter({'apple': 3, 'banana': 2, 'orange': 1})
Example: Finding Most Common Elements
fruits = ["apple", "banana", "orange", "banana", "apple", "apple"]
counter = Counter(fruits)
print(counter.most_common(2)) # Top 2 most common elements
Output:
[('apple', 3), ('banana', 2)]
3. defaultdict
– Handling Missing Keys
A defaultdict
is a dictionary that assigns a default value to missing keys.
Example: Using defaultdict
with int
from collections import defaultdict
numbers = defaultdict(int) # Default value is 0
numbers["a"] += 1
print(numbers)
Output:
defaultdict(<class 'int'>, {'a': 1})
Example: Grouping Data with defaultdict
data = [("Alice", "Math"), ("Bob", "Science"), ("Alice", "English")]
subjects = defaultdict(list)
for name, subject in data:
subjects[name].append(subject)
print(subjects)
Output:
defaultdict(<class 'list'>, {'Alice': ['Math', 'English'], 'Bob': ['Science']})
4. OrderedDict
– Maintaining Key Order
An OrderedDict
maintains the insertion order of keys (Python 3.7+ does this for normal dictionaries too).
Example: Using OrderedDict
from collections import OrderedDict
ordered_dict = OrderedDict()
ordered_dict["a"] = 1
ordered_dict["b"] = 2
ordered_dict["c"] = 3
print(ordered_dict)
Output:
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
5. deque
– Fast Appends and Pops
A deque
(double-ended queue) allows fast appends and pops from both ends, unlike a list.
Example: Using deque
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])
Example: Removing Elements from deque
d.pop() # Removes from the right
d.popleft() # Removes from the left
print(d)
Output:
deque([1, 2, 3])
6. namedtuple
– Readable Tuples with Names
A namedtuple
provides named fields for tuples, making them more readable.
Example: Defining a namedtuple
from collections import namedtuple
Person = namedtuple("Person", ["name", "age"])
p = Person("Alice", 25)
print(p.name, p.age)
Output:
Alice 25
Example: Accessing Elements Like a Dictionary
print(p._asdict()) # Converts namedtuple to dictionary
Output:
{'name': 'Alice', 'age': 25}
7. ChainMap
– Merging Multiple Dictionaries
A ChainMap
combines multiple dictionaries into one logical view.
Example: Merging Configuration Settings
from collections import ChainMap
default_settings = {"theme": "light", "language": "English"}
user_settings = {"language": "French"}
settings = ChainMap(user_settings, default_settings)
print(settings["language"]) # Takes the value from user_settings
Output:
French
8. Performance Comparison
Data Structure | Use Case | Performance Benefit |
---|---|---|
Counter | Counting elements | Faster than loops |
defaultdict | Handling missing keys | Avoids KeyErrors |
OrderedDict | Keeping key order | Useful in older Python versions |
deque | Fast appends/removals | Faster than lists |
namedtuple | Readable tuples | Lightweight and structured |
ChainMap | Combining dictionaries | Avoids merging manually |
9. When to Use the collections
Module?
✅ Use Counter when you need to count occurrences.
✅ Use defaultdict when handling missing keys.
✅ Use OrderedDict when order matters.
✅ Use deque when you need fast queue operations.
✅ Use namedtuple for structured, readable tuples.
✅ Use ChainMap to merge dictionaries dynamically.
🚫 Avoid overcomplicating your code with collections
unless necessary.