Skip to content

Python collections Module: Detailed Overview and Examples

The collections module in Python provides alternatives to the built-in data types, offering specialized container datatypes that are designed to make certain programming tasks easier and more efficient. This module includes several classes that enhance the functionality of standard data types such as lists, tuples, and dictionaries.

Importing the collections Module

To use the functionalities provided by the collections module, you first need to import it:

import collections

1. namedtuple()

The namedtuple() function returns a new type of tuple with named fields. It helps to create tuple subclasses with named fields that can be accessed like attributes.

Example

from collections import namedtuple

# Define a named tuple
Person = namedtuple('Person', 'name age')

# Create an instance of the named tuple
person = Person(name='Alice', age=30)

# Access fields by name
print(person.name)  # Output: Alice
print(person.age)   # Output: 30

2. deque

The deque class provides a double-ended queue which supports appending and popping elements from both ends efficiently. It is ideal for queue and stack implementations.

Example

from collections import deque

# Create a deque
d = deque([1, 2, 3])

# Append to both ends
d.append(4)        # Right end
d.appendleft(0)    # Left end

# Pop from both ends
right_end = d.pop()
left_end = d.popleft()

print(d)           # Output: deque([1, 2, 3])
print(right_end)   # Output: 4
print(left_end)    # Output: 0

3. Counter

The Counter class is a subclass of dict that helps to count hashable objects. It provides a convenient way to tally occurrences of items in a collection.

Example

from collections import Counter

# Create a Counter object
c = Counter('abracadabra')

# Get counts of individual characters
print(c)  # Output: Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1})

# Most common elements
print(c.most_common(2))  # Output: [('a', 5), ('b', 2)]

4. OrderedDict

The OrderedDict class is a dictionary that maintains the order of its items. Prior to Python 3.7, regular dictionaries did not maintain order.

Example

from collections import OrderedDict

# Create an OrderedDict
od = OrderedDict()
od['one'] = 1
od['two'] = 2
od['three'] = 3

# Items maintain insertion order
for key, value in od.items():
    print(f"{key}: {value}")

# Output:
# one: 1
# two: 2
# three: 3

5. defaultdict

The defaultdict class provides a default value for nonexistent keys, avoiding KeyError exceptions when accessing or modifying dictionary elements.

Example

from collections import defaultdict

# Create a defaultdict with default value of list
d = defaultdict(list)

# Append to the defaultdict
d['key1'].append(1)
d['key1'].append(2)
d['key2'].append(3)

print(d['key1'])  # Output: [1, 2]
print(d['key2'])  # Output: [3]
print(d['key3'])  # Output: []  # Default empty list

6. ChainMap

The ChainMap class groups multiple dictionaries or mappings into a single view. It allows you to search through several mappings at once.

Example

from collections import ChainMap

# Create multiple dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Create a ChainMap
chain_map = ChainMap(dict1, dict2)

print(chain_map['a'])  # Output: 1
print(chain_map['b'])  # Output: 2  # First dictionary value for 'b'
print(chain_map['c'])  # Output: 4

7. UserDict, UserList, and UserString

These classes are wrappers around dictionary, list, and string objects, respectively. They allow for easier customization and extension of these data types.

Example of UserDict

from collections import UserDict

# Create a custom dictionary class
class MyDict(UserDict):
    def __setitem__(self, key, value):
        print(f"Setting {key} = {value}")
        super().__setitem__(key, value)

# Use the custom dictionary
d = MyDict()
d['key'] = 'value'
# Output: Setting key = value

Example of UserList

from collections import UserList

# Create a custom list class
class MyList(UserList):
    def append(self, item):
        print(f"Appending {item}")
        super().append(item)

# Use the custom list
lst = MyList()
lst.append(1)
# Output: Appending 1

Example of UserString

from collections import UserString

# Create a custom string class
class MyString(UserString):
    def upper(self):
        return f"MyString: {self.data.upper()}"

# Use the custom string
s = MyString("hello")
print(s.upper())  # Output: MyString: HELLO

Conclusion

The collections module offers powerful and flexible data structures that can simplify coding tasks and improve code efficiency. Each class is designed to handle specific use cases:

  • namedtuple() for creating lightweight, immutable objects with named fields.
  • deque for fast appends and pops from both ends of a sequence.
  • Counter for counting hashable objects and finding common elements.
  • OrderedDict for maintaining order in dictionaries (though this is now the default behavior in Python 3.7+).
  • defaultdict for handling missing dictionary keys with default values.
  • ChainMap for managing multiple mappings as a single unit.
  • UserDict, UserList, and UserString for customizing and extending built-in container types.

Understanding these tools can help you write more efficient, readable, and maintainable code.