A Simple Introduction to Python’s Collections module
The collection module in python is one of the most underrated modules of python. It can be pretty handy when dealing with iterable data.
Introduction
Collection module includes many container datatypes. In this post lets learn these data types and its capabilities.
- counter
- deque
- namedtuple
- OrderedDict
Counter
A Counter is an unordered collection that keeps track of how many times equivalent values are added. It works on iterator type object and returns a Counter Object, which operates similarly to a python-dict. Its really useful in getting the count of objects in few leetcode questions. Examples are as follows.
from collections import Counter_list = ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'A', 'A', 'a', 'a', 'b']
_string = 'my name is Marshall mathers'counter_made_with_list = Counter(_list)
counter_made_with_string = Counter(_string)print('Counter Made with List',counter_made_with_list)
print('Counter Made with String',counter_made_with_string)
And the output comes out to be:
Counter Made with List Counter({'A': 6, 'B': 3, 'a': 2, 'b': 1})
Counter Made with String Counter({' ': 4, 'a': 4, 'm': 3, 's': 3, 'e': 2, 'r': 2, 'h': 2, 'l': 2, 'y': 1, 'n': 1, 'i': 1, 'M': 1, 't': 1})
It is really important to know that Counter will count each and every object including space. Also ‘M’ and ‘m’ are counted different.
One can also use dictionary methods like pop and insert and one really good method is ‘subtract’, which
print(counter_made_with_list - counter_made_with_string)Counter({'A': 6, 'B': 3, 'b': 1})
dequeu -easy queue system
Deque is short form for ‘double-ended queue’. It is a type of stack/queue which is appends and pops from either side of the deque i.e. left or right with approximately the same O(1) performance in either direction.
from collections import dequetest_queue = deque()#Normal Appending
test_queue.append('Random Element')
test_queue.append('Normal append')
print(test_queue)#Appending from left
test_queue.appendleft('Append from Left')
print(test_queue)# Normal pop
test_queue.pop()
print(test_queue)#Pop from left
test_queue.popleft()
print(test_queue)
deque is also thread-safe i.e can share common attributes between forked threads, and more memory efficient. Few helpful methods in dequeue are remove, reverse, rotate.
Namedtuple
Namedtuple is a key-value container type similar to dict. But if we have dict in python, why use something else? In dicts, only the keys have to be hashable, not the values. Namedtuples don’t have keys, so hashing isn’t an issue.
Tuples are immutable, whether named or not. namedtuple
makes the access more convenient, by using names instead of indices. You can only use valid identifiers for namedtupl
. Few examples and methods are as follows:
from collections import namedtuple# Created a Car namedtuple with attributes type, color and style
Car = namedtuple('Car', ['name','type','color','style'])cybertruck = Car('cybertruck','Electric', 'Silver', 'Pickup')
bmw = Car('bmw','Diesel', 'White', 'SUV')print('Car is',bmw[0],bmw[1],bmw[2],bmw[3])
print('Another car is',cybertruck.name,cybertruck.type,cybertruck.color, cybertruck.style)
The standard tuple unpacking also works with namedtuple. I often use if for class-like representation of objects and it's very handy when accessing pandas data in the form of a tuple.
Check out few of my other articles and follow me for more python, machine learning and cryptocurrencies.