Collections in Python are containers used for storing data and are commonly known as data structures, such as lists, tuples, arrays, dictionaries, etc.
Python has a built-in collections module providing additional data structures for collections of data.
There are 6 most commonly used data structures in the collections modules.
Defaultdict is exactly like a dictionary in python. The only difference is that it does not give an exception/key error when you try to access the non-existent key.
In the following code, even though the 4th index was not initialized, the compiler still returns a value, 0, when we try to access it.
from collections import defaultdictnums = defaultdict(int)nums['one'] = 1nums['two'] = 2nums['three'] = 3print(nums['four'])
Counter is a built-in data structure which is used to count the occurrence of each value present in an array or list.
The following code is counting the number of occurrences of the value 2 in the given list.
from collections import Counterlist = [1,2,3,4,1,2,6,7,3,8,1,2,2]answer=Counter()answer = Counter(list)print(answer[2])
Deque is an optimal version of list used for inserting and removing items. It can add/remove items from either start or the end of the list.
In the following code, z is being added at the end of the given list and g is at the start of the same list.
from collections import deque#initializationlist = ["a","b","c"]deq = deque(list)print(deq)#insertiondeq.append("z")deq.appendleft("g")print(deq)#removaldeq.pop()deq.popleft()print(deq)
The Namedtuple() solves a very major problem in the field of computer science. Usual tuples need to remember the index of each field of a tuple object, however, namedtuple() solves this by simply returning with names for each position in the tuple.
In the following code, an index is not required to print the name of a student rather passing an attribute is sufficient for the required output.
from collections import namedtupleStudent = namedtuple('Student', 'fname, lname, age')s1 = Student('Peter', 'James', '13')print(s1.fname)
ChainMap combines a lot of dictionaries together and returns a list of dictionaries. ChainMaps basically encapsulates a lot of dictionaries into one single unit with no restriction on the number of dictionaries.
The following program ChainMap to return two dictionaries.
import collectionsdictionary1 = { 'a' : 1, 'b' : 2 }dictionary2 = { 'c' : 3, 'b' : 4 }chain_Map = collections.ChainMap(dictionary1, dictionary2)print(chain_Map.maps)
OrderedDict is a dictionary that ensures its order is maintained. For example, if the keys are inserted in a specific order, then the order is maintained. Even if you change the value of the key later, the position will remain the same.
from collections import OrderedDictorder = OrderedDict()order['a'] = 1order['b'] = 2order['c'] = 3print(order)#unordered dictionaryunordered=dict()unordered['a'] = 1unordered['b'] = 2unordered['c'] = 3print("Default dictionary", unordered)