×

Search anything:

Different container Data Structures of Python’s Collections Module

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

There are so many modules that you may have heard about or used in Python. Have you heard about the Collections module?

As a programmer, data structures is a very common word in our world. Different programming languages have their own set of data structures. In Python, it is a bit different but very easy to work with. Even as a beginner you can easily visualize a list, dictionary and a set. These are the data structures in python. What's important is know their properties.

First comes the list. As the name suggests its more like an array that stores items. But these items cannot have duplicate values and can be accessed using indexing like in arrays. Tuple is also very similar to a list, just that it is immutable. What does this mean? Well, we cannot change the values inside a tuple once declared but in a list, we can. A dictionary is very similar to the dictionary we use in real life: there is a word and next to it we have its meaning. Likewise in python dictionaries we have a key and its value. Key can be considered the word and value as its meaning.

Although we have these data structures available, these have restricted functionalities. To be able to access the advanced functionalities of these data structures, we have the Collections module. The different types of data structures available in Collections module are:

  • Named Tuple
  • Counter
  • Deque
  • Default dictionary
  • Ordered Dictionary
  • ChainMap

We cannot directly use the functions on collection module without importing it to our system.The following command is used to do this.

import collections

So, what does this module actually consist of?

p

These are the classes enclosed in the collections module. Don't worry. We will discuss about all of this below.

But first you need to know how to use these classes in your program. Python doesn't know where these classes are defined unless you specify it. We know it is inside the collections module, so we need to tell python to load it from the collections module that we imported to our program earlier.

This is how we do it.

from collections import *classname*

for example:

# importing Counter class
from collections import Counter

1. namedtuple( )


We know what a tuple is and we know that to access any specific element inside a tuple we need to do indexing. But if the number of elements is very large, it can be very tiresome to remember the index for each element. So an easier approach is to provide an identifier for each element.

This is where we use the namedtuple class of collection module. It returns a tuple with a named entry, meaning a name is assigned to each value in the tuple. This is much easier to use since it does not require accessing specific elements using the index values. We can convert the immutable tuple in python to the namedtuple by assigning the name to all values present in that tuple.


How It Works?

from collections import namedtuple
Fruits = namedtuple('Fruits', 'name, season')  
f1 = Fruits('Orange', 'Winter')  
print(f1.name) 

Output:

Orange

Above we can see that the first entry in out Fruits tuple suggests the name of the fruit i.e Orange and next is the value that returns the season in which it ripens i.e Winter. So to access the value winter we dont need to remember its index in the tuple, but we can just call it using command f1.name where f1 is the object we have assigned this tuple to.

2. Counter


Dictionary, we have already discussed above what it is and how it works in python. The Counter is a subclass of dictionary object which helps to count hashable objects. The Counter collections allow us to keep the count of all the items which are inserted into a collection with the keys. It is the unordered collection where items are stored as dictionary keys, and their counts are stored as the dictionary values.

How It Works?

from collections import Counter  
list = [10,10,12,10,12,15,13,13,10,15,9,11,12,11,12] 
listcount = Counter(list)  
print(listcount) 
character = Counter('Hello, Enjoy great Learning')
print(character[e])

Output:

listcount({10: 4, 12: 4, 11: 2, 13: 2, 15: 2, 9: 1})
3

Here, we have used Counter on both the list and string. In the list, the Counter classs has counted total number of each element present in the list. In the string also, we have displayed the total number of letter 'e' present in the given sentence.

3. Deque

Stacks and Queues are some very important data structures in programming languages like C and C++. Stack works using the LIFO(last in first out) method while the queue follows the FIFO(first in first out) mechanism. A Deque can be seen as double-ended queue, meaning we can add and remove the items from both the ends. We can visualize it as a cynlindrical pipe that is open from both ends, such that items can be added or removed from both ends. Deque is basically the generalization of stacks and queues .It enhances the capabilities of a stack or a queue.

How it works?

from collections import deque  
#initialization
mylist = ["a","b","c"]  
newlist = deque(mylist)  
print(deq)  

#insertion
newlist.append("m")  
newlist.appendleft("I")  
print(newlist)

#removal
newlist.pop()  
newlist.popleft()  
print(newlist)

Output:

newlist(['a', 'b', 'c'])
newlist(['I', 'a', 'b', 'c', 'm'])
newlist(['a', 'b', 'c'])

4. defaultdict()

The defaultdict is the subclass of the built-in dictionary class. It has the same functionality as that of dictionary, except it has an advantage. Like in the below given example, when we tried to access an undefined key, we got an output integer value 0. However, in case of normal dictionaries we would expect a KeyError.

How it works?

from collections import defaultdict  
values = defaultdict(int)  
values['first'] = 1  
values['second'] = 2
values['third'] = 3 
print(values['fourth']) 

Output:

0

5. OrderedDict


An OrderedDict is a dictionary subclass that remembers the order in which that keys were first inserted. For eg: if we insert items in order A,B,C,D then when we try to access these items OrderedDict will give us an output in same order as A,B,C,D. However, in case of normal dictionaries this may not be followed. It may return us values in any order as long as it fulfills the criteria of output.

How it works?

from collections import OrderedDict 

print("This is a Dict:\n") 
dictionary = {} 
dictionary['Hello'] = 1
dictionary['Learner'] = 2
dictionary['How'] = 3
dictionary['Are'] = 4
dictionary['you'] = 5

for key, value in dictionary.items(): 
    print(key) 

print("\nThis is an Ordered Dict:\n") 
ordereddict = OrderedDict() 
ordereddict['Hello'] = 1
ordereddict['Learner'] = 2
ordereddict['How'] = 3
ordereddict['Are'] = 4
ordereddict['you'] = 5

for key, value in ordereddict.items(): 
    print(key) 

Output:

This is a Dict:

you
Learner
Are
How
Hello

This is an Ordered Dict:

Hello
Learner
How
Are
you

6. ChainMap

Lastly, suppose you have your required output distributed in different dictionaries, but you want one combined output or want to merge these values into one dictionary. Python allows you to do this using the ChainMap class in Collections module. It returns a list of dictionaries. Suppose you have two dictionaries with several key value pairs, in this case ChainMap will make a single list with both the dictionaries in it. We use keys() function to access the keys of a ChainMap and values() to access the values of elements.

How it works?

from collections import ChainMap
dictionary_1 = { 'Hello' : 1, 'Learner' : 2 } 
dictionary_2 = { 'How' : 3, 'are' : 4, 'you' : 5 }  
newdictionary = ChainMap(dictionary_1, dictionary_2)  
print (list(newdictionary.keys()))
print (list(newdictionary.values())) 

Output:

['Learner', 'Hello', 'How', 'you', 'are']
[2, 1, 3, 5, 4]

Conclusion

With that, we conclude our tutorial on Collections module. We have discussed all the important data structures in the collection module.

from collections import ChainMap
a = { 1: 'apple' , 2: 'banana'}
b = {3: 'lemon' , 4: 'ginger'}
c = ChainMap(a,b)
print (list(c.keys()))

What will be the output of the above given code?


[1,2,3,4]
{1,2,3,4}
["apple","banana","lemon","ginger"]
"apple","banana","lemon","ginger"
Correct answer is 1. The ChainMap combines dictionary a and b.We are then displaying the keys of this new combined set of dictionaries in form list.

Different container Data Structures of Python’s Collections Module
Share this