What is the Python collections module?
Published on July 23, 2023
Python collections
Module
The Python collections
module is a built-in module that provides alternative data structures to the built-in data types, such as lists, tuples, sets, and dictionaries. It offers specialized container datatypes that can be more efficient and convenient for specific use cases.
Notable Data Structures in collections
1. namedtuple
- A subclass of tuples with named fields. Useful for creating simple classes to hold data records.
from collections import namedtuple
# Create a namedtuple
Student = namedtuple('Student', 'fname, lname, age')
s1 = Student('Peter', 'James', '13')
print(s1.fname)
2. deque
- A double-ended queue that supports efficient addition and removal of elements from both ends. Useful for implementing queues and stacks.
from collections import deque
# Initialize a deque
my_list = ["a", "b", "c"]
deq = deque(my_list)
print(deq)
3. Counter
- A dictionary subclass for counting hashable objects. It allows you to quickly count occurrences of items in an iterable.
from collections import Counter
# Create a Counter to count occurrences in a list
my_list = [1, 2, 3, 4, 1, 2, 6, 7, 3, 8, 1, 2, 2]
answer = Counter(my_list)
print(answer[2])
4. OrderedDict
- A dictionary subclass that maintains the order of items as they were added. Useful for tasks that require a specific order.
from collections import OrderedDict
# Create an OrderedDict
order = OrderedDict()
order['a'] = 1
order['b'] = 2
order['c'] = 3
print(order)
5. defaultdict
- A dictionary subclass that provides a default value for nonexistent keys. It simplifies working with dictionaries and avoids KeyError exceptions.
from collections import defaultdict
# Create a defaultdict with int as the default value type
nums = defaultdict(int)
nums['one'] = 1
nums['two'] = 2
nums['three'] = 3
print(nums['four'])
6. ChainMap
- A dictionary-like class that can combine multiple mappings into a single view. Useful for managing configurations or settings.
import collections
# Create a ChainMap by chaining dictionaries
dictionary1 = {'a': 1, 'b': 2}
dictionary2 = {'c': 3, 'b': 4}
chain_Map = collections.ChainMap(dictionary1, dictionary2)
print(chain_Map.maps)
7. UserDict
- A wrapper class for creating custom dictionary-like classes.
8. UserList
- A wrapper class for creating custom list-like classes.
9. UserString
- A wrapper class for creating custom string-like classes.
When to Use collections
The collections
module is particularly useful when you need specialized data structures beyond the basic built-in types. It can help you write more efficient and readable code for various tasks, such as data manipulation, counting, and maintaining order.
Whether you're working on algorithms, data analysis, or any other Python project, the collections
module provides a valuable set of tools for managing and processing data effectively.