Collections Module in Python

Hello everyone, In this tutorial, we’ll be learning about the Collections Module in Python which is very handy when we want to perform some special kind of operations with the most popular built-in datatypes that we have in Python like list, dict, tuple, and set. Let us jump into the tutorial with a brief overview of what we’ll be discussing all throughout this tutorial.

Overview of the Collections Module in Python

Collections are the container that stores a collection of data that can be both similar or different. Collections Module Provide us some data structures that are based upon these built-in containers and are created to add more to their functionalities. Let us discuss the 6 main types of container datatypes that are:-

  1. namedtuple()
  2. deque
  3. ChainMap
  4. Counter
  5. OrderedDict
  6. defaultDict

Let us discuss each of them one by one.

namedtuple() in Python

This will return a subclass of tuple whose each position has a name associated with it. Their main advantage is that we can access elements in the tuple with the name given rather than the positional index. Because we are getting a class from namedtuple() we need to initialize an instance of it with the arguments that we want to pass. See the code below to get an understanding of how it works.

from collections import namedtuple

height = float(input("Enter Height of Triangle: "))
base = float(input("Enter Base of Triangle: "))
area = 0.5*height*base

Triangle = namedtuple('Triangle','height,base,area')
t = Triangle(height,base,area)
print("------- Triangle Information -------")
print("\nHeight: ",t.height)
print("\nBase: ",t.base)
print("\nArea: ",t.area)

Explanation of the Code: We are printing the tuple elements using the name associated with it.

  1. First, we import the namedtuple from the collections module.
  2. Then we have defined three variables named height, base, and area that we already know how to do.
  3. Next, we define our namedtuple which will take two arguments-
    • typename – It is the name of the subclass of the tuple. (In our case, Triangle)
    • field names – These are the positional naming for the namedtuple for ex. in our case, index 0 is named as height and so on.
  4. Now because we have created a class we need to create an instance to use it with the arguments. (t is the name of the instance that we have created).
  5. All set, Now we can ask the information about any element in the tuple with its field name.

Let’s check the output of the code above.

Enter Height of Triangle: 20
Enter Base of Triangle: 10
------- Triangle Information -------

Height:  20.0

Base:  10.0

Area:  100.0

Creating namedtuple instance using the list

We can create a namedtuple instance by using a list of the arguments rather than giving the arguments separately. We can achieve this using the _make() method.

Replacing the Field Values

We can replace or change the field values for an instance by using the previous instance that we have already created using _replace() method. This will create a new instance with the changed value and will not affect the value of the original instance.

Let see these abilities using a program.

Triangle = namedtuple('Triangle','height,base')
t1 = Triangle._make([20,10])
t2 = t1._replace(height = 10,base = 50)
t = [t1,t2]
for item in t:
    print("------- Triangle Information -------")
    print("\nHeight: ",item.height)
    print("\nBase: ",item.base)
------- Triangle Information -------

Height:  20

Base:  10
------- Triangle Information -------

Height:  10

Base:  50

deque in Python

This will return an object of deque class with the data that is given to it or the data from the iterable. The data is arranged from left to right manner and it has capabilities of both stacks and queues that’s why it is called a double-ended queue. therefore we can insert or pop elements from either side and also can insert an element at a specified position. Let us define a deque and perform operations on it one by one.

Importing deque and initializing

from collections import deque

list1 = ['Apple','Banana','Mango']
list2 = ['Peach','Grapes',]
list3 = ['Pineapple']

deck = deque(list1)
print(deck)
print(type(deck))

We have created 3 lists and initialize our deque with one of the lists. Let see the output of the above code to check whether or deque object is created or not.

deque(['Apple', 'Banana', 'Mango']) 
<class 'collections.deque'>

Appending elements to left and right of our deque

We can append or add elements in a deque at the end (extreme right) and start (extreme left) positions using append() and appendleft() methods respectively.

deck.append('Orange')
deck.appendleft('Cherry')
print("After appending elements to right & left: ",deck)
After appending elements to right & left:
 deque(['Cherry', 'Apple', 'Banana', 'Mango', 'Orange'])

Extending our deque from left and right using iterables

We can extend our deque by adding other iterables at the end (extreme right) and start (extreme left) positions using extend() and extendleft() methods respectively.

deck.extend(list2)
deck.extendleft(list3)
print("After extending iterables to right & left:\n",deck)
After extending iterables to right & left:
deque(['Pineapple', 'Cherry', 'Apple', 'Banana', 'Mango', 'Orange', 'Peach', 'Grapes'])

Inserting an element in deque at a specified position

We can insert an element at any valid index in our deque by adding by using insert() method.

deck.insert(6,'Blueberries')
print("After inserting fruit at index 6:\n",deck)
After inserting fruit at index 6:
 deque(['Pineapple', 'Cherry', 'Apple', 'Banana', 'Mango', 'Orange', 'Blueberries', 'Peach', 'Grapes'])

Removing elements from the end and start of our deque

We can remove or pop elements in a deque from the end (extreme right) and start (extreme left) positions using pop() and popleft() methods respectively.

deck.pop()
deck.popleft()
print("After popping rightmost & leftmost elements:\n",deck)
After popping rightmost & leftmost elements:
 deque(['Cherry', 'Apple', 'Banana', 'Mango', 'Orange', 'Blueberries', 'Peach'])

Removing a specified element from our deque

We can remove or pop any desired element that is present in the deque by using remove() method. This method will remove only the first occurrence of the specified element if it is present more than 1 time from the leftmost side.

deck.remove('Apple')
print("After removing a specified element:\n",deck)
After removing a specified element:
 deque(['Cherry', 'Banana', 'Mango', 'Orange', 'Blueberries', 'Peach'])

Rotating the deque with defined no. of rotations

We can rotate our deque in anticlockwise and clockwise directions by using the rotate() method. This method will take an integer as an argument that defines the total no. of rotations to be performed.

  • A positive integer means clockwise rotation
  • A negative integer means anti-clockwise rotation
deck.rotate(2)
print("After rotation to 2 places:\n",deck)
After rotation to 2 places:
 deque(['Blueberries', 'Peach', 'Cherry', 'Banana', 'Mango', 'Orange'])

Reversing the deque

We can reverse our deque by using the reverse() method.

deck.reverse()
print("After reversing the deck:\n",deck)
After reversing the deck:
 deque(['Orange', 'Mango', 'Banana', 'Cherry', 'Peach', 'Blueberries'])

Clearing the deque

The clear() function will remove all elements from our deque and returns an empty deque.

deck.clear()
print("After clearing the deck:\n", deck)
After clearing the deck:
 deque([])

ChainMap in Python

This is a data structure that combines several dictionaries and converts them into a list as a single unit. Searching for a key in the new mapping is done until that key is found if present. It means that write, update and deletion operations only operate on the first key found if that key is present in the mapping more than once.

Importing ChainMap and creating first Mapping

from collections import ChainMap

d1 = {'Apple':1, 'Banana':3, 'Mango':2}
d2 = {'Peach':2,'Mango':4}

mapping  = ChainMap(d1, d2)
print("New mapping as a list:\n",mapping.maps)
New mapping as a list:
 [{'Apple': 1, 'Banana': 3, 'Mango': 2}, {'Peach': 2, 'Mango': 4}]

We get the new Mapping as a list that combines the dictionaries.

Getting the keys and values from the Mapping

Using the keys() and values() methods, the keys and the values present in the new mapping can be printed. Note that the keys() and values() will give us a KeysView and ValuesView of the mapping, therefore, we have to convert them into a list type.

print(list(mapping.keys()))
print(list(mapping.values()))
['Peach', 'Mango', 'Apple', 'Banana']
[2, 2, 1, 3]

Notice that value of key Mango is 2 which is in the d1 dictionary this is because if a key is present in more than one value then ChainMap takes the value of the key from the dictionary that appears first or which is mapped first.

Adding more dictionaries to existing ChainMap

We can add more dictionary into our existing mapping and also by using the new_child() method. The dictionary will be inserted at the start of the ChainMap and also notice some other changes in the keys and values of the new mapping that we get from the code below.

d3 = {'Mango':1,'Guava':7}
new_mapping = mapping.new_child(d3)
print(new_mapping.maps)

print(list(new_mapping.keys()))
print(list(new_mapping.values()))
[{'Mango': 1, 'Guava': 7}, {'Apple': 1, 'Banana': 3, 'Mango': 2}, {'Peach': 2, 'Mango': 4}]
['Peach', 'Mango', 'Apple', 'Banana', 'Guava']
[2, 1, 1, 3, 7]

Counter, defaultDict, and OrderedDict in Python

These are other types of useful Container data-types provided in the Collections Module in Python that is covered in the following tutorials.

To learn about Counter, refer to this tutorial –

Counter in Python

To learn about the defaultDict and OrderedDict, refer to this tutorial –

Defaultdict, OrderedDict from Collections module of Python

We hope you like this tutorial and if you have any doubts, feel free to ask in the comment tab below.

You may like to read.

Change Keys of Dictionary in Python

Find the Middle element of Linked List in Python

Tuples in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *