Python’s `collections` module provides high-performance container datatypes beyond the built-in types `list`, `dict`, and `tuple`.
Here’s how you can use some of its functions:
1. namedtuple(): Returns a tuple-like object with fields accessible by attribute lookup as well as being indexable and iterable.
```
from collections import namedtuple
Car = namedtuple(‘Car’, [‘color’, ‘mileage’])
my_car = Car(‘red’, 1821.25)
print(my_car.color)
print(my_car.mileage)
```
1. deque: A list-like container with fast appends and pops on either end.
```
from collections import deque
d = deque()
d.append(‘a’)
d.append(‘b’)
d.append(‘c’)
print(f“The deque is : {str(d)}”)
print(‘popped value is : ‘, d.pop())
print(‘deque after popping : ‘, d)
```
1. ChainMap: A dictionary-like class to create a single view of multiple mappings.
```
from collections import ChainMap
d1 = {‘a’: 1, ‘b’: 2}
d2 = {‘c’: 3, ‘d’: 4}
d3 = ChainMap(d1, d2)
print(d3.maps)
```
1. Counter: A dict subclass for counting hashable objects.
```
from collections import Counter
cnt = Counter()
list = [1,2,3,3,2,1,2,3,1,2,3,1,1,1]
cnt = Counter(list)
print(cnt)
```
1. OrderedDict: A dict subclass that remembers the order entries were added.
```
from collections import OrderedDict
numbers = OrderedDict([(‘one’, 1), (‘two’, 2), (‘three’, 3)])
print(f“The ordered dict is : {str(numbers)}”)
```
1. defaultdict: A dict subclass that calls a factory function to supply missing values.
```
from collections import defaultdict
counts = defaultdict(int)
cities = [‘New York’, ‘Beijing’, ‘New York’, ‘London’, ‘Beijing’]
for city in cities:
counts[city] += 1
print(dict(counts))
```
1. UserDict: A more or less complete user-defined wrapper around dictionary objects.
1. UserList: A more or less complete user-defined wrapper around list objects.
1. UserString: A more or less complete user-defined wrapper around string objects.