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:

Python
>>> 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:

Python
>>> 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:

Python
>>> 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:

Python
>>> 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.

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.

intermediate data-structures python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated June 23, 2025