Python Advanced - Data Structures with Collections Module
Exploring advanced data structures with the Collections module in Python
The Collections module in Python provides specialized container datatypes that provide alternatives to Python’s general-purpose built-in containers like dict, list, set, and tuple. This tutorial explores advanced data structures available in the Collections module.
Key Points:
- The Collections module provides specialized container datatypes.
- These data structures offer alternatives to Python’s built-in containers.
- Using these specialized data structures can improve code clarity and performance.
Counter
A Counter is a dictionary subclass for counting hashable objects. Here is an example:
from collections import Counter
# Creating a Counter
counter = Counter(['a', 'b', 'c', 'a', 'b', 'b'])
print(counter) # Output: Counter({'b': 3, 'a': 2, 'c': 1})
# Accessing elements
print(counter['a']) # Output: 2
print(counter['d']) # Output: 0 (default value for missing keys)
defaultdict
A defaultdict is a dictionary subclass that calls a factory function to supply missing values. Here is an example:
from collections import defaultdict
# Creating a defaultdict
default_dict = defaultdict(int)
default_dict['a'] += 1
print(default_dict) # Output: defaultdict(, {'a': 1})
print(default_dict['b']) # Output: 0 (default value from int factory)
OrderedDict
An OrderedDict is a dictionary subclass that maintains the order of entries. Here is an example:
from collections import OrderedDict
# Creating an 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)])
# Reordering the OrderedDict
ordered_dict.move_to_end('b')
print(ordered_dict) # Output: OrderedDict([('a', 1), ('c', 3), ('b', 2)])
deque
A deque (double-ended queue) is a list-like container with fast appends and pops on either end. Here is an example:
from collections import deque
# Creating a deque
dq = deque([1, 2, 3])
dq.append(4)
dq.appendleft(0)
print(dq) # Output: deque([0, 1, 2, 3, 4])
# Popping elements
dq.pop()
dq.popleft()
print(dq) # Output: deque([1, 2, 3])
namedtuple
A namedtuple is a factory function for creating tuple subclasses with named fields. Here is an example:
from collections import namedtuple
# Creating a namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p) # Output: Point(x=1, y=2)
# Accessing elements
print(p.x) # Output: 1
print(p.y) # Output: 2
ChainMap
A ChainMap is a dictionary-like class for creating a single view of multiple mappings. Here is an example:
from collections import ChainMap
# Creating two dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
# Creating a ChainMap
chain_map = ChainMap(dict1, dict2)
print(chain_map) # Output: ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4})
# Accessing elements
print(chain_map['b']) # Output: 2 (from the first dictionary)
print(chain_map['c']) # Output: 4 (from the second dictionary)
UserDict, UserList, UserString
The collections module provides wrapper classes around dictionary, list, and string objects to make it easier to create custom variants. Here is an example:
from collections import UserDict, UserList, UserString
# Creating a custom dictionary
class MyDict(UserDict):
def __setitem__(self, key, value):
super().__setitem__(key, value*2)
custom_dict = MyDict()
custom_dict['a'] = 3
print(custom_dict) # Output: {'a': 6}
# Creating a custom list
class MyList(UserList):
def append(self, item):
super().append(item*2)
custom_list = MyList([1, 2, 3])
custom_list.append(4)
print(custom_list) # Output: [1, 2, 3, 8]
# Creating a custom string
class MyString(UserString):
def __str__(self):
return self.data.upper()
custom_string = MyString("hello")
print(custom_string) # Output: HELLO
Summary
In this tutorial, you learned about exploring advanced data structures with the Collections module in Python. The Collections module provides specialized container datatypes such as Counter, defaultdict, OrderedDict, deque, namedtuple, ChainMap, and wrapper classes like UserDict, UserList, and UserString. Understanding how to use these data structures can help improve code clarity and performance in various applications.