In python, collections are the four container data types- list, tuple, set, and dictionary, which is known as built-in collections. Each of these collection data types has its strengths and weaknesses. To overcome these weaknesses, python developers have come up with a python collections module.
Before diving deep into the collections module in python, we must understand what exactly is meant by the term module?
A module is nothing but a python file with a .py extension that we can call in another python program/file. This file/module consists of python statements, functions that can be called on another program by directly importing the module.
Now that we know the meaning of the module let’s understand more about some of the most important data types in the collections module.
Some Important Data Types used of python collections Module:
1. Namedtuple() in Python
It is a mixture of dictionary and tuple. Like a dictionary because we declare key-value pairs in it, and like tuple, they are immutable. By immutable, it means they cannot be changed, or we can understand them as read-only data types.
A question may arise that why we use them? Many times it becomes challenging to work with indexes as we cannot remember which type of value is stored at which index. This task can be solved by namedtuple().
The difference between python collection- namedtuple() and a dictionary is mutable (we can change values in it), but sometimes we don’t want that to happen. Like let’s say we don’t want the user to be able to change values; sometimes, there we can use namedtuple().
For using this data type, we must import it from the collections module.
from collections import namedtuple
employee= namedtuple('Employee','name age gender')
ashwini= employee('Ashwini',20,'male')
print(ashwini)
print(ashwini.name)
print(ashwini.gender)
print(ashwini.age)
OUTPUT- Employee(name='Ashwini', age=20, gender='male') Ashwini male 20
We can also use for loop to traverse through the tuple.
for i in ashwini:
print(i)
Output- Ashwini 20 Male
2. Defaultdict in Python
In python built-in collections, we have a dictionary to store key-value pairs. But if we try to retrieve a value that does not exist in that dictionary, it gives KEY ERROR, which can sometimes create a problem. To overcome this problem in the python collections module, we have ‘defaultdict’ datatype where if we pass a key that is non-existent in the dictionary, we get default value as an output.
First, we need to import it from the python collections module.
from collections import defaultdict
Now we need to create an object. It contains one argument – default value. Here we can specify a data type something like int, float, str, or we can make a function that returns a value and put this function as an argument to this object.
dd=defaultdict(float)
dd['ashwini']=20
dd['anuj']=19
dd['john']=44
Now when we try to give a value to the defalutdict that is not present-
print(dd["simran"])
We get,
Output- 0.0
But if this was a regular dictionary like this,
D={'ashwini':20, ‘anuj':19}
print(D["simran"])
Output- KeyError
Now, if we want to give a default value, we need to create a function and call that function inside the object.
dd=defaultdict(default_value)
dd['ashwini']=20
dd['anuj']=19
dd['john']=44
def default_value():
return 25
Now if we try this,
print(dd["simran"])
Output- 25
3. Deque() in Python
Deque or double-ended queue is a data type implemented by python collections module and is used to do operations like insertion and deletion from both ends. It can perform both of these operations quite quickly as it has a time complexity of O(1).
Some Common Operations that can be performed on deque:
- append()
- pop()
- appendleft()
- popleft()
from collections import deque
a=[1,2,3,4,5,6,7]
b=deque(a)
b.append(8)
print(b)
print(b.pop())
b.appendleft(0)
print(b.popleft())
print(b)
4. Counter in Python
This data type keeps the count of all the items in an iterable and returns a dictionary with count as value and that item as key.
Output- deque([1, 2, 3, 4, 5, 6, 7,8]) 8 deque([0, 1, 2, 3, 4, 5, 6, 7]) 0 deque([1, 2, 3, 4, 5, 6, 7])
from collections import Counter
list1=[1,2,3,4,1,2,3,4,4,5]
x=Counter(list1)
print(x)
#Counter({1:2,2:2,3:2,4:3,5:!})
Some functions of Counter :
- elements() – It returns all the values of the iterable.
- most_common() – It returns the items in descending order based on the counts.
from collections import Counter
list1=[1,2,3,4,1,2,3,4,4,5,5,5,5]
x=Counter(list1)
print(x)
print(x.most_common())
elements=x.elements()
print(list(elements))
Output- Counter({5: 4, 4: 3, 1: 2, 2: 2, 3: 2}) [(5, 4), (4, 3), (1, 2), (2, 2), (3, 2)] [1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5]
5. ChainMap in Python
We can use ChainMap to combine several dictionaries and returns a list containing all the dictionaries.
dict1={1:2,3:4,5:6}
dict2={2:1,4:3,6:5}
X= ChainMap(dict1,dict2)
print(x)
print(x[6])
Output- ChainMap({1: 2, 3: 4, 5: 6}, {2: 1, 4: 3, 6: 5}) 5
If we want to get all the keys and values of ChainMap, we can use keys() and values(), respectively.
print(list(x.keys()))
print(list(x.values()))
[1, 3, 5, 2, 4, 6] [2, 4, 6, 1, 3, 5]
6. OrderedDict in Python
OrderedDict was mainly used before python version 3.5.
It learns the order of input, and even if the value is changed, the same order is followed.
After python version 3.7, every dictionary is OrderedDict. Therefore, the use of OrderedDict has been reduced.
But we will learn about it as it is still a part of the Python collections module.
Before Python Version 3.5:
d={}
d['a'] = 1
d['b'] = 2
d['c'] = 3
a 1 c 3 b 2
for key, value in d.items():
print(key, value)
from collections import OrderedDict
od=OrderedDict()
od['a']=1
od['b']=2
od['c']=3
for key, value in od.items():
print(key, value)
Output- a 1 b 2 c 3
Must Read:
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
We have most of the concepts of the python collections module in detail. The data types in this module have lots of applications. They solve a lot of shortcomings in built-in python collections. Because of modules like this, python is one of the easiest and useful languages.
Try to run the programs on your side and let me know if you have any queries.
Happy Coding!