Image by editor
Python has a module named . collection There are various types of containers. A container is a Python object that consists of various objects and implements a way to retrieve and iterate over those objects.
In this tutorial, collection module.
- counter
- OrderedDict
- DefaultDict
- chain map
- Named Tuple
- dequeue
A counter is a dict subclass, used to hold the number of repeatable elements in an unordered dictionary where the keys represent the elements and the values represent the number of repeatable elements
To initialize the counter object, we use the counter() function, which can be called in the following ways:
from collections import Counter
1) Use a sequence of items
counter = Counter(['D','C','C','E','E','E','A','A','X']) print(counter)
output:
Counter({'D': 1, 'C': 2, 'E': 3, 'A': 2, 'X': 1})
2) Use of dictionaries
counter = Counter({'X':3,'Y':2,'Z':1}) print(counter)
output:
Counter({'X': 3, 'Y': 2, 'Z': 1})
3) Using keyword arguments
counter = Counter(X=3,Y=2,Z=1) print(counter)
output:
Counter({'X': 3, 'Y': 2, 'Z': 1})
Ordered dictionaries are a subclass of dictionaries, but unlike dictionaries, they remember the order in which keys were inserted
from collections import OrderedDict orderdict = OrderedDict() orderdict["x"] = 2 orderdict["y"] = 3 orderdict["z"] = 4 print(orderdict)
output:
OrderedDict([('x', 2), ('y', 3), ('z', 4)])
1) When inserting a new item into an existing ordered dictionary, the new item is added to the end of the dictionary, thus preserving the order of the dictionary.
orderdict["v"] = 5 print(orderdict)
output:
OrderedDict([('x', 2), ('y', 3), ('z', 4), ('v', 5)])
2) Removing an item from an existing ordered dictionary and inserting the same item again inserts that particular item at the end of the dictionary.
del orderdict["x"] print(orderdict)
output:
OrderedDict([('y', 3), ('z', 4), ('v', 5)])
orderdict["x"] = 2 print(orderdict)
output:
OrderedDict([('y', 3), ('z', 4), ('v', 5), ('x', 2)])
3) When reassigning or updating the values of existing key-value pairs in an OrderedDict object, the keys keep their positions, but the keys get new values.
orderdict.update(z = "four") print(orderdict)
output:
OrderedDict([('y', 3), ('z', 'four'), ('v', 5), ('x', 2)]
DefaultDict is a dict subclass that provides default values for keys that never exist, so it never raises a keyError
from collections import defaultdict
1) using a list as defaultdict
dftdict = defaultdict(list) for i in range(3): dftdict[i].append(i) print(dftdict)
output:
defaultdict(list, {0: [0], 1: [1], 2: [2]})
2) use int as defaultdict
intdict = defaultdict(int) X = [1,2,3,4,5,1,1,3,4,5] for i in X: #The default value is 0 so there is no need to enter the key first intdict[i] += 1 print(intdict)
output:
defaultdict(int, {1: 3, 2: 1, 3: 2, 4: 2, 5: 2})
ChainMap returns a list of dictionaries as it is used to combine multiple dictionaries into a single unit
from collections import ChainMap x1 = {'a': 0, 'b': 1} x2 = {'c':2,'d':3} x3 = {'e':4,'f':5} chainmap = ChainMap(x1,x2,x3) print(chainmap)
output:
ChainMap({'a': 0, 'b': 1}, {'c': 2, 'd': 3}, {'e': 4, 'f': 5})
1) Access the value using the key name
output:
2) access to values
output:
ValuesView(ChainMap({'a': 0, 'b': 1}, {'c': 2, 'd': 3}, {'e': 4, 'f': 5}))
3) Access to keys
output:
KeysView(ChainMap({'a': 0, 'b': 1}, {'c': 2, 'd': 3}, {'e': 4, 'f': 5}))
4) insert new dictionary
A new dictionary is inserted at the beginning of the ChainMap using the new_child() method.
chainmap1 = chainmap.new_child({'g':6,'h':7}) print(chainmap1)
output:
ChainMap({'g': 6, 'h': 7}, {'a': 0, 'b': 1}, {'c': 2, 'd': 3}, {'e': 4, 'f': 5})
A NamedTuple is a tuple object with a name in every position.
from collections import namedtuple
1) declaration of namedtuple
band = namedtuple('Country',['name','capital','famousband'])
2) Inserting values into a namedtuple
val = band("south korea","Seoul","BTS") print(val)
output:
Country(name="south korea", capital="Seoul", famousband='BTS')
3) Access the value using the index
output:
4) Access the value using its name
output:
A double-ended queue is a list for implementing add and pop operations on both sides of a container
from collections import deque
1) deque declaration
queue = deque([4,5,6]) print(queue)
output:
2) use append to insert the element on the right side which is the end of the deque
queue.append(7) print(queue)
output:
3) Use append to insert elements to the left, at the beginning of the deque
queue.appendleft(3) print(queue)
output:
4) Use pop to remove elements from the right end of the deque
output:
5) Use popleft to remove elements from the left, which is the beginning of the deque
queue.popleft() print(queue)
output:
Priya Sengar (Moderate, github) is a data scientist at Old Dominion University. Priya is passionate about solving data problems and transforming them into solutions.