collections
The Python collections
module provides specialized container data types that extend the capabilities of Python’s built-in containers, such as lists, tuples, sets, and dictionaries.
This module introduces additional data structures that are particularly useful for handling complex data scenarios.
Here’s a quick example:
>>> from collections import Counter
>>> Counter("mississippi")
Counter({'i': 4, 's': 4, 'p': 2, 'm': 1})
Key Features
- Provides high-performance, specialized container data types
- Includes
namedtuple
for creating tuple subclasses with named fields - Implements
deque
for fast, memory-efficient appends and pops from both ends of a sequence - Offers
Counter
for counting hashable objects - Includes
OrderedDict
for maintaining the order of keys - Provides
defaultdict
for dictionaries with default values - Contains
ChainMap
for linking multiple mappings
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
collections.namedtuple |
Function | A factory function for creating tuple subclasses with named fields |
collections.deque |
Class | A list-like container with fast appends and pops on either end |
collections.Counter |
Class | A dictionary subclass for counting hashable objects |
collections.OrderedDict |
Class | A dictionary subclass that remembers the order entries were added |
collections.defaultdict |
Class | A dictionary subclass that calls a factory function to supply missing values |
collections.ChainMap |
Class | A class for linking multiple mappings into a single unit |
Examples
Creating a named tuple:
>>> from collections import namedtuple
>>> Point = namedtuple("Point", ["x", "y"])
>>> point = Point(11, y=22)
>>> point.x, point.y
(11, 22)
Using deque
for efficient appends and pops:
>>> from collections import deque
>>> numbers = deque([1, 2, 3])
>>> numbers.append(4)
>>> numbers.popleft()
1
>>> list(numbers)
[2, 3, 4]
Common Use Cases
- Creating tuple-like classes with
namedtuple
for better code readability - Using
deque
for implementing queues and stacks efficiently - Counting elements in an iterable with
Counter
- Maintaining insertion order with
OrderedDict
- Providing default values for dictionary keys with
defaultdict
- Combining multiple dictionaries with
ChainMap
Real-World Example
Suppose you need to count the occurrence of words in a text and maintain them in order by frequency. You can achieve this using Counter
and OrderedDict
:
>>> from collections import Counter, OrderedDict
>>> text = "lorem ipsum dolor sit amet ipsum lorem"
>>> word_counts = Counter(text.split())
>>> ordered_word_counts = OrderedDict(word_counts.most_common())
>>> ordered_word_counts
OrderedDict(
[
('lorem', 2),
('ipsum', 2),
('dolor', 1),
('sit', 1),
('amet', 1)
]
)
In this example, the collections
module helps you efficiently count word occurrences and maintain them in order.
Related Resources
Tutorial
Python's collections: A Buffet of Specialized Data Types
In this tutorial, you'll learn all about the series of specialized container data types in the collections module from the Python standard library.
For additional information on related topics, take a look at the following resources:
- Write Pythonic and Clean Code With namedtuple (Tutorial)
- Python's deque: Implement Efficient Queues and Stacks (Tutorial)
- OrderedDict vs dict in Python: The Right Tool for the Job (Tutorial)
- Using the Python defaultdict Type for Handling Missing Keys (Tutorial)
- Custom Python Dictionaries: Inheriting From dict vs UserDict (Tutorial)
- Python's Counter: The Pythonic Way to Count Objects (Tutorial)
- Custom Python Lists: Inheriting From list vs UserList (Tutorial)
- Writing Clean, Pythonic Code With namedtuple (Course)
- Write Pythonic and Clean Code With namedtuple (Quiz)
- Using OrderedDict in Python (Course)
- Handling Missing Keys With the Python defaultdict Type (Course)
- Counting With Python's Counter (Course)
By Leodanis Pozo Ramos • Updated June 23, 2025