Last Updated On By Anmol Lohana
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.
Table of Contents
The Python counter is a built-in data structure/container that we use to count the occurrence of each element present in the container.
Counter(container)
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")
The Python OrderedDict is a built-in data structure/container same as dictionary. It ensures that the order of dictionary is maintained.
from collections import OrderedDict
od = OrderedDict()
od[1] = 'a'
od[2] = 'n'
od[3] = 'm'
od[4] = 'o'
od[5] = 'l'
print(od)
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.
from collections import defaultdict
nums = defaultdict(int)
nums['one'] = 1
nums['two'] = 2
nums['three'] = 3
print(nums['zero'])
The Python ChainMap is a built-in function that combines multiple dictionaries into a single list.
ChainMap(dictionaries separated by comma)
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)
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.
from collections import namedtuple
person = namedtuple('Person',['name','age','DOB'])
p = person('Anmol','22','05-Mar-1998')
print(p.name)
DeQue Python stands for Double Ended Queue. We use this container to add or remove items from both side of the container.
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)
UserDict is the same as Python dictionary. We use it when we want to create our dictionary with some modifications or new features.
from collections import UserDict
d = {'a':1, 'b': 2, 'c': 3}
add = UserDict(d)
print(add.data)
add = UserDict()
print(add.data)
Python UserList is the same as the list. We use it when we want to create our list with some modifications or new features.
from collections import UserList
List = [1, 2, 3, 4]
L = UserList(List)
print(L.data)
L = UserList()
print(L.data)
UserString is the same as string. We use it when we want to create our string with some modifications or new features.
from collections import UserString
d = "Welcome to Code Leaks."
s = UserString(d)
print(s.data)
s = UserString("Empty")
print(s.data)
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.