In this tutorial, you will learn all about the Python collections module with the help of examples. The collections module in Python is used to enhance the functionalities of the existing Python built-in collections containers.
Before going through this article, you should have basic knowledge of the following Python topics.
Collections in Python are the containers that are used to store collections of items for example list, tuple, dictionary, and set are examples of containers. These all are Python built-in containers but Python provides various modules to enhance the functionalities of the containers. One such module in Python is the collections module.
Headings of Contents
Python collections Module
The python collections module is a built-in Python module that provides various data structures to store the collections of the item and perform operations on them.
Python Collections Module Containers
Container | Description |
---|---|
Counter | Subclass of dict used to count hashable objects |
defaultdict | dict subclass that calls a factory function to supply missing values |
OrderedDict | dict subclass that remembers the order of the key. |
deque | list like container that fast appends and removes items from both sides |
ChainMap | Creating a list of dictionaries ( Combine multiple dictionaries) |
namedtuple | Factory function for creating tuple subclass with named fields |
The Counter
Counter in the collection module is the sub-class of the Python dictionary. The Counter() function takes an iterable ( iterable may be string, list, etc) as an argument and returns a dictionary. In the dictionary, the key is the element in the iterable, and the value represent the number of time the key present in the iterable.
To use Counter() in class, you have to import it from the collections module.
from collection import Counter
Create Counter object
After importing the Counter() class from the collection module you have to create an object of the Counter() class.
counter = Counter()
There are multiple ways to create the Counter.
- Using object
from collections import Counter # create the object counter = Counter() string = "Programming Funda" for i in string: counter[i] += 1 print("The Result is:- ", counter) #Output: #The Result is:- Counter({'r': 2, 'g': 2, 'a': 2, 'm': 2, 'n': 2, 'P': 1, 'o': 1, 'i': 1, ' ': 1, 'F': 1, 'u': 1, 'd': 1})
Without object
To create the counter without an object, you have to pass iterable directly to the Counter() class as an argument.
from collections import Counter lst = [1,2,3,3,3,4,4] print("The Result is:- ", Counter(lst))
Output
The Result is:- Counter({3: 3, 4: 2, 1: 1, 2: 1})
Methods
Counter() class support three methods. let’s see.
- elements()
The elements() function is used to return all the items from the Counter object.
Example:
from collections import Counter # create the object counter = Counter() string = "Programming" for i in string: counter[i] += 1 print(list(counter.elements())) #Output #['P', 'r', 'r', 'o', 'g', 'g', 'a', 'm', 'm', 'i', 'n']
- most_common()
The most_common() function returns a list of tuples and each tuple contains an item and the total presence of that item in the iterable. The list is sorted based on the count of the elements.
Example
from collections import Counter # create the object counter = Counter() string = "Programming" for i in string: counter[i] += 1 print(counter.most_common()) # Output # [('r', 2), ('g', 2), ('m', 2), ('P', 1), ('o', 1), ('a', 1), ('i', 1), ('n', 1)]
- subtract() function
The subtract() function takes iterable as an argument and subtracts element count using that argument.
Example
from collections import Counter # create the object counter = Counter() lst = [1,2,2,2,1,3,3] for i in lst: counter[i] += 1 sbt = {2:1, 3:1} print("Before subtract:- ", counter) counter.subtract(sbt) print("After subtract:- ", counter)
Output
Before subtract:- Counter({2: 3, 1: 2, 3: 2})
After subtract:- Counter({1: 2, 2: 2, 3: 1})
The defaultdict
The defaultdict works the same as the Python dictionary. The only difference between both is python dictionary throw KeyError when the key does not exist but defaultdict does not.
To create defaultdict dictionary, you can pass data type to the defaultdict() constructor as an argument that is also called default_factory.
To use defaultdict() constructor you have to import it from the collections module.
Example
from collections import defaultdict # create the create dc = defaultdict(int) dc["one"] = 10 dc["two"] = 20 dc["three"] = 30 dc["four"] = 40 print("The result is:- ", dc) # access first value print(dc["one"]) # access value whose key not exists. print(dc["five"])
Output
The result is:- defaultdict(<class 'int'>, {'one': 10, 'two': 20, 'three': 30, 'four': 40}) 10 0
As you can see in the above example, we passed int in defaultdict() constructor as the default_factory. Next, we defined the value for key four (4) but when we try to access the value of key five that does not exists, it returns 0.
In a normal dictionary, when you will try to access the value of a key that does not exist, you will get a KeyError
.But defaultdict
initialize the new key with the default_factory’s default value which is 0.
The OrderedDict
The OrderedDict is the subclass of the dictionary. The OrderedDict is a dictionary in the Python collections module that maintains the order of the key in which they are inserted, which means if you change the value of the key, it will not change the position of the key.
You have to import OrderedDict from the collections module.
You can create an OrderedDict object with the help of the OrderedDict() constructor. In the below example, we have created the OrderedDict object and inserted item one by being into it.
Example
from collections import OrderedDict # Create the object od = OrderedDict() # string to inserted string = "Python" # inserted record in dictionary for i in range(1, len(string) + 1): od[i] = string[i - 1] print("The Dictionary:- ", od)
Output
The Dictionary:- OrderedDict([(1, 'P'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n')])
Now you can access each item of the dictionary od using Python for a loop.
for key, value in od.items():
print(key, value)
Methods
- popitem(Last=True)
# Create the object
od = OrderedDict()
string = "Python"
for i in range(1, len(string) + 1):
od[i] = string[i - 1]
print("Before :- ", od)
print(od.popitem())
print("After :- ", od)
Output
Before :- OrderedDict([(1, 'P'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n')])
(6, 'n')
After :- OrderedDict([(1, 'P'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o')])
The Deque
The Deque is a list optimized for performing insertion and deletion of items from both sides.
First, you have to import the deque from the collections module. To create a deque object you have to pass a list inside the deque constructor as an argument.
Example
from collections import deque
lst = [1,2,3,4,5]
# create deque object
deq = deque(lst)
print("The result is:- ", deq)
Output
The result is:- deque([1, 2, 3, 4, 5])
Insert elements to deque
You can easily insert items to the deq from both sides: right and left. To add items from the right side, use the append() method, And if you want to insert items from the left side, you have to use appendleft() method.
Example
from collections import deque
lst = [1,2,3,4,5]
# create deque object
deq = deque(lst)
print("The result is:- ", deq)
# append item to the right
deq.append("Funda")
# append item to the left
deq.appendleft("Programming")
print("The result is:- ", deq)
Output
The result is:- deque([1, 2, 3, 4, 5]) The result is:- deque(['Programming', 1, 2, 3, 4, 5, 'Funda'])
Remove elements from the deque
Remove elements from the deque is a similar process to inserting items into the deque.Instead of using append() and appendleft() method, you have to use pop() and popleft() method.
pop() is used to remove the item from the last of the list and return the removed item.
popleft() method is used to remove the item from the left of the list and it also returns the removed item.
Example
from collections import deque
lst = [1,2,3,4,5]
# create object
deq = deque(lst)
print("The result is:- ", deq)
# remove item from the right
deq.pop()
# remove item to the left
deq.popleft()
print("The result is:- ", deq)
Clear a deque
To remove all the items from the deque, you have to use the clear() method.
Example
from collections import deque
lst = [1,2,3,4,5]
# create object
deq = deque(lst)
print("The result is:- ", deq)
# clear the deque
deq.clear()
print("The result is:- ", deq)
Output
The result is:- deque([1, 2, 3, 4, 5])
The result is:- deque([])
Counting elements in the deque
Sometimes we want to total a number of occurrences for a specified item into the list, Then we can use the count() function and pass the item to count() as an argument.
Example
from collections import deque lst = [1,2,3,4,5,4, 3, 3] print(lst.count(5)) print(lst.count(4)) print(lst.count(3))
Output
1 2 3
The ChainMap
The ChainMap is used to combine several dictionaries or mappings and return a list of dictionaries. To use ChainMap you have to import it from the Python collections module.
Example
from collections import ChainMap first = {'one': 1, "two":2, "three": 3} second = {'four': 4, "five":5, "six": 6} third = {'seven': 7, "eight":8, "nine": 9} # combine all the dictionaries result = ChainMap(first, second, third)
Output
The result is:- ChainMap({'one': 1, 'two': 2, 'three': 3}, {'four': 4, 'five': 5, 'six': 6}, {'seven': 7, 'eight': 8, 'nine': 9})
Keys of the ChainMap
You can get the list of all the keys from ChainMap using the keys() function.
Example
from collections import ChainMap first = {'one': 1, "two":2, "three": 3} second = {'four': 4, "five":5, "six": 6} third = {'seven': 7, "eight":8, "nine": 9} # combine all the dictionaries result = ChainMap(first, second, third) # key of the chainmap print(list(result.keys()))
Output
['seven', 'eight', 'nine', 'four', 'five', 'six', 'one', 'two', 'three']
Values of the ChainMap
You can get the list of all the values from ChainMap using the values() function.
Example
from collections import ChainMap first = {'one': 1, "two":2, "three": 3} second = {'four': 4, "five":5, "six": 6} third = {'seven': 7, "eight":8, "nine": 9} # combine all the dictionaries result = ChainMap(first, second, third) # key of the chainmap print(list(result.values()))
Adding new dictionary to ChainMap
After creating ChainMap, you have another option to add new another dictionary by using the new_child() function.
Example
from collections import ChainMap first = {'one': 1, "two":2, "three": 3} second = {'four': 4, "five":5, "six": 6} third = {'seven': 7, "eight":8, "nine": 9} # combine all the dictionaries result = ChainMap(first, second, third) five = {"ten": 10} print("The Result is:- ", result.new_child(five))
Output
The Result is:- ChainMap({'ten': 10}, {'one': 1, 'two': 2, 'three': 3}, {'four': 4, 'five': 5, 'six': 6}, {'seven': 7, 'eight': 8, 'nine': 9})
You notice that a new dictionary is added to the beginning of the ChainMap.
The namedtuple
The namedtuple returns a tuple object with the name of each position. The difference between both tuples is that in a normal tuple, you have to remember the index number for each item to access but in namedtuple you don’t have to remember the index number because it provides a name for each index number.
Import namedtuple
To use namedtuple you have to import it from the collections module.
Example
from collections import namedtuple Student = namedtuple('Student','first_name, last_name, age,email') s1 = Student('Vishvajit','Rao', 20,'[email protected]') print(s1)
Output
Student(first_name='Vishvajit', last_name='Rao', age=20, email='[email protected]')
Now you can access the value by using the name of the position.
Example
from collections import namedtuple Student = namedtuple('Student','first_name, last_name, age,email') s1 = Student('Vishvajit','Rao', 20,'[email protected]') print(s1.first_name) print(s1.last_name) print(s1.age) print(s1.email)
Create a new instance using existing
The _asdict() function is used to create a new OrderedDict instance by existing instance.
Example
from collections import namedtuple Student = namedtuple('Student','first_name, last_name, age,email') s1 = Student('Vishvajit','Rao', 20,'[email protected]') od = s1._asdict() print("The Result is:- ", od)
Changing fields value by _replace() function
To change the value of the fields of the instance, the _replace() function is used. You have to remember one thing is that, _replace() function creates a new instance, it does not change the existing instance.
Example
from collections import namedtuple Student = namedtuple('Student','first_name, last_name, age,email') s1 = Student('Vishvajit','Rao', 20,'[email protected]') print(s1) result = s1._replace(age=30) print(result) Output: Student(first_name='Vishvajit', last_name='Rao', age=20, email='[email protected]') Student(first_name='Vishvajit', last_name='Rao', age=30, email='[email protected]')
Conclusion
In this article, you have learned all about the Python collections module with the help of examples. Collections modules in Python provide lots of containers to store collections of items.
Basically, the Python collections module is used to enhance the functionalities of the existing Python built-in containers like List, Tuple, Set, Dictionary, etc.
If you want to perform different operators with Python collections that do not provide a built-in list, tuple, set, string and dictionary, Then you can use the Python collections module.
I hope this article will have helped you if you like this article. please share and keep visiting for further Python tutorials.
Python Collections Module Reference:- Click Here
Thanks for reading…