Python Collections: Upgraded Version of Built-in Collections?

In Python, collections are the four container data types- list, tuple, set, and dictionary- 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 and 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 in Python collections Module: 

1. Namedtuple() in Python

It is a mixture of a dictionary and a tuple. Like a dictionary, because we declare key-value pairs in it, they are immutable like a tuple. By immutable, it means they cannot be changed, or we can understand them as read-only data types.  

A question may arise: why do 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 a 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, we can use namedtuple(). 

To use 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)  
python collections
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) 
Printing Tuples
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 the 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 
Default Dict

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"]) 
collections
Output - KeyError  

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 
Default Dict

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) 
python comments

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 the Counter: 

  1. elements() – It returns all the values of the iterable. 
  1. most_common() : 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)) 
Example of Collections
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 return 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) 
OrderedDict
Output-
a 1 
b 2 
c 3 

Must Read:

Conclusion

We have detailed explanations of most of the concepts of the Python Collections module. 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 most useful languages.

Try to run the programs on your side, and let me know if you have any queries.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments