Python collections module gives various types of containers. A Python container is an object or data structure that we use to store the data. And it is a built-in module that contains some built-in containers in it like a list, tuple, dictionary, etc.
In this article, we are going to discuss the Python containers given below in detail.
- Counters
- OrderedDict
- DefaultDict
- ChainMap
- NamedTuple
- DeQue
- UserDict
- UserList
- UserString
Table of Contents
1. Python Counters
The Python counter is a built-in data structure/container that we use to count the occurrence of each element present in the container.
Syntax
Counter(container)
Example Code
from collections import Counter
list = [1,2,3,4,5,5,4,3,2,1]
count=Counter(list)
print("5 occurs ", count[5] , "times in the container")
Output

2. Python OrderedDict
The Python OrderedDict is a built-in data structure/container same as dictionary. It ensures that the order of dictionary is maintained.
Example Code
from collections import OrderedDict
od = OrderedDict()
od[1] = 'a'
od[2] = 'n'
od[3] = 'm'
od[4] = 'o'
od[5] = 'l'
print(od)
Output

3. Python DefaultDict
The DefaultDict is a built-in data structure/container same as dictionary in Python. But, it does not throw any exception or key error. If we are trying to access the value that is not defined it will assign value to that key by default.
Example Code
from collections import defaultdict
nums = defaultdict(int)
nums['one'] = 1
nums['two'] = 2
nums['three'] = 3
print(nums['zero'])
Output

4. Python ChainMap
The Python ChainMap is a built-in function that combines multiple dictionaries into a single list.
Syntax
ChainMap(dictionaries separated by comma)
Example Code
from collections import ChainMap
p = {'Name': 'Anmol', 'Age': '22'}
p_i = {'Age': '22', 'Roll_no': 'F16sw75'}
c_m = list(ChainMap(p_i, p))
print(c_m)
Output

5. Python NamedTuple
The NamedTuple is a built-in function that we use to access the tuple object by its name instead of accessing it by index in Python. Because, memorizing the indices is difficult so we can use NamedTuple to access items of tuple by their names.
Example Code
from collections import namedtuple
person = namedtuple('Person',['name','age','DOB'])
p = person('Anmol','22','05-Mar-1998')
print(p.name)
Output

6. Python DeQue
DeQue Python stands for Double Ended Queue. We use this container to add or remove items from both side of the container.
Example Code
from collections import deque
list = ["Hello", "Hey", "Hi"]
deq = deque(list)
print("Original list ", deq)
deq.append("World")
deq.appendleft("!")
print("Extented list ", deq)
deq.pop()
deq.popleft()
print("Removed list ", deq)
deq.pop()
deq.popleft()
print("Again Removed list ", deq)
Output

7. Python UserDict
UserDict is the same as Python dictionary. We use it when we want to create our dictionary with some modifications or new features.
Example Code
from collections import UserDict
d = {'a':1, 'b': 2, 'c': 3}
add = UserDict(d)
print(add.data)
add = UserDict()
print(add.data)
Output

8. Python UserList
Python UserList is the same as the list. We use it when we want to create our list with some modifications or new features.
Example Code
from collections import UserList
List = [1, 2, 3, 4]
L = UserList(List)
print(L.data)
L = UserList()
print(L.data)
Output

9. Python UserString
UserString is the same as string. We use it when we want to create our string with some modifications or new features.
Example Code
from collections import UserString
d = "Welcome to Code Leaks."
s = UserString(d)
print(s.data)
s = UserString("Empty")
print(s.data)
Output

Conclusion
In this article, we discussed Python Collections Module. We can use any module by importing it, so we import this module too. Collections are data structure/containers; and we observed nine different containers to understand the Python collection module.