27
Getting Started with Collections Module in Python
In This Article we will go through various example covering the data structures containers offered by python collections module.
The collections module provide us with various different container datatypes, which are used to stored objects, and
access them, iterate over the objects, these all container datatypes , all have there usecases, which we will look later in the Article.
access them, iterate over the objects, these all container datatypes , all have there usecases, which we will look later in the Article.
Before going forward, i'd like to mention one more point, all the container datatypes are built on top of the existing datatypes available in python like (Tuple,List,Set,Dict), these are introduced to improve the functionalities of the built-in container datatype in python.
As we progess further in the Article things will get clear to you.
As we progess further in the Article things will get clear to you.
We will going to the look at containers available in collections module :-
to use containers available in collections module, you will first have to import them,
there are mutiple ways to import :-
there are mutiple ways to import :-
#Import all container at once
from collections import *
#Import single container
from collections import Counter
# Importing and accessing using alias
import collections as clns
counter = clns.Counter()
Example :-
from collections import Counter
names = ['George', 'Paul', 'George', 'Ringo', 'John', 'John', 'George', 'John', 'Ringo', 'Paul', 'Ringo', 'Ringo', 'George', 'Paul', 'John', 'John', 'John', 'John', 'John', 'Paul']
names_counter = Counter(names)
print(names_counter)
Output :-
Counter({'John': 8, 'George': 4, 'Paul': 4, 'Ringo': 4})
We can see that it return Counter object , which we can cast to dict for further use.
Counter container has most_common method which returns List the n most common elements and their counts from the most common to the least. If n is None, then list all element counts.
calling most_common() on names_counter will return in :-
names_counter.most_common()
Output :-
[('John', 8), ('George', 4), ('Paul', 4), ('Ringo', 4)]
where the most frequent element with its count is at the from and least frequent element with its count at the end of the list
to know more about other methods available in Counter use can call help(counter_object) which will give you documentation for Counter
to know more about other methods available in Counter use can call help(counter_object) which will give you documentation for Counter
Example :-
# Regular dict
reg_dict = dict()
reg_dict['apples'] = 3
reg_dict['pineapple'] = 5
reg_dict['mango'] = 1
reg_dict['orange'] = 7
reg_dict['grapes'] = 2
print(f"Regular Dict {reg_dict} \n\n")
from collections import OrderedDict
# Ordered Dict
ord_dict = OrderedDict()
ord_dict['apples'] = 3
ord_dict['pineapple'] = 5
ord_dict['mango'] = 1
ord_dict['orange'] = 7
ord_dict['grapes'] = 2
print(f"Ordered Dict {ord_dict}")
Regular Dict {'apples': 3, 'pineapple': 5, 'mango': 1, 'orange': 7, 'grapes': 2}
Ordered Dict OrderedDict([('apples', 3), ('pineapple', 5), ('mango', 1), ('orange', 7), ('grapes', 2)])
Deleting elment from OrderedDict :-
del ord_dict['mango']
print(ord_dict)
Output :-
OrderedDict([('apples', 3), ('pineapple', 5), ('orange', 7), ('grapes', 2)])
Inserting element in OrderedDict :-
ord_dict['mango'] = 11
print(ord_dict)
Output :-
OrderedDict([('apples', 3), ('pineapple', 5), ('orange', 7), ('grapes', 2), ('mango', 11)])
As we can see first we deleted the key mango from OrderedDict then re-inserted it at the end of the OrderedDict.
for more method use can call help(OrderedDict_object)
for more method use can call help(OrderedDict_object)
Example :-
from collections import defaultdict
d_dict = defaultdict(lambda : "Not Available") # passing default value
d_dict['John'] = 11
d_dict['Paul'] = 12
d_dict['George'] = 9
d_dict['Rango'] = 14
print(d_dict['Rango'])
print(d_dict['John'])
print(d_dict['max'])
Output :-
14
11
Not Available
values() :- This function is used to display values of all the dictionaries in ChainMap.
maps() :- This function is used to display keys with corresponding values of all the dictionaries in ChainMap.
Example :-
from collections import ChainMap
d1 = {'Ram': 12, 'Shyam': 12}
d2 = {'Mango': 3, 'Orange': 4}
d3 = {'Car': 4, 'Bus': 8}
# Defining the chainmap
c = ChainMap(d1, d2, d3)
print(c)
Output :-
ChainMap({'Ram': 12, 'Shyam': 12}, {'Mango': 3, 'Orange': 4}, {'Car': 4, 'Bus': 8})
# printing chainMap using maps
print ("All the ChainMap contents are : ")
print (c.maps)
# printing keys using keys()
print ("All keys of ChainMap are : ")
print (list(c.keys()))
# printing keys using keys()
print ("All values of ChainMap are : ")
print (list(c.values()))
Output :-
All the ChainMap contents are :
[{'Ram': 12, 'Shyam': 12}, {'Mango': 3, 'Orange': 4}, {'Car': 4, 'Bus': 8}]
All keys of ChainMap are :
['Car', 'Bus', 'Mango', 'Orange', 'Ram', 'Shyam']
All values of ChainMap are :
[4, 8, 3, 4, 12, 12]
Example :-
from collections import namedtuple
Book = namedtuple('Book', ['title', 'author', 'price'])
b = Book('Xyz', 'xyz', 50)
print("accessing using index")
print(b[0])
print(b[1])
print(b[2])
print("\n------------------\n")
print("accessing using name")
print(b.title)
print(b.author)
print(b.price)
print("\n------------------\n")
print("using getattr() ")
print(getattr(b, 'title'))
print(getattr(b, 'author'))
print(getattr(b, 'price'))
Output :-
accessing using index
Xyz
xyz
50
------------------
accessing using name
Xyz
xyz
50
------------------
using getattr()
Xyz
xyz
50
Example :-
from collections import deque
# initializing deque
dq = deque(['John','Max','Harry','Marcus','Robin','Jackub'])
# using append() to insert element at right end
dq.append("Jimmy")
print ("The deque after appending at right is : ")
print (dq,"\n")
# using appendleft() to insert element at left end
dq.appendleft('Alex')
print ("The deque after appending at left is : ")
print (dq,"\n")
# using pop() to delete element from right end
dq.pop()
print ("The deque after deleting from right is : ")
print (dq,"\n")
# using popleft() to delete element from left end
dq.popleft()
print ("The deque after deleting from left is : ")
print (dq,"\n")
Output :-
The deque after appending at right is :
deque(['John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub', 'Jimmy'])
The deque after appending at left is :
deque(['Alex', 'John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub', 'Jimmy'])
The deque after deleting from right is :
deque(['Alex', 'John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub'])
The deque after deleting from left is :
deque(['John', 'Max', 'Harry', 'Marcus', 'Robin', 'Jackub'])
We have completed 6 containers available in the collections module , there are other container datatype available in collections module you can check by clicking the link below :-
other collections module containers
other collections module containers
Hope you guys have learned all the topics covered in this Article :-)
27