Introduction to Python
Collections

Collections in Python

Collections

In Python, collections are containers that are used to store collections of data, for example, list, dict, set, tuple, etc. These are built-in collections. Several modules have been developed that provide additional data structures to store collections of data. One such module is the Python Collections Module.

Python Collections Module

The Python Collections Module provides alternatives to built-in data types that are more powerful, faster, and have more features. The module implements specialized container datatypes providing alternatives to Python's general-purpose built-in containers like dict, list, set, and tuple.

Counter

The Counter class in Python is a subclass of the dict class. It is used to count the number of elements in an iterable. The Counter class is a part of the collections module.

from collections import Counter
 
# List of elements
elements = ['a', 'b', 'c', 'a', 'b', 'a']
 
# Count the number of elements
count = Counter(elements)
 
print(count)
$ python main.py
Counter({'a': 3, 'b': 2, 'c': 1})

defaultdict

The defaultdict class in Python is a subclass of the dict

class. It is used to provide a default value for a key that does not exist. The defaultdict class is a part of the collections module.

from collections import defaultdict
 
# Define a defaultdict
d = defaultdict(int)
 
# Add elements to the defaultdict
d['a'] = 1
d['b'] = 2
 
print(d['a'])
print(d['b'])
print(d['c'])
$ python main.py
1
2
0

deque

The deque class in Python is a subclass of the list class. It is used to create a double-ended queue. The deque class is a part of the collections module.

 
from collections import deque
 
# Define a deque
d = deque([1, 2, 3])
 
# Add elements to the deque
d.append(4)
d.appendleft(0)
 
print(d)
$ python main.py
deque([0, 1, 2, 3, 4])

namedtuple

The namedtuple class in Python is a subclass of the tuple class. It is used to create a tuple with named fields. The namedtuple class is a part of the collections module.

from collections import namedtuple
 
# Define a namedtuple
Point = namedtuple('Point', ['x', 'y'])
 
# Create a Point object
p = Point(1, 2)
 
print(p.x)
print(p.y)
$ python main.py
1
2

OrderedDict

The OrderedDict class in Python is a subclass of the dict class. It is used to create a dictionary that remembers the order of the keys. The OrderedDict class is a part of the collections module.

from collections import OrderedDict
 
# Define an OrderedDict
d = OrderedDict()
 
# Add elements to the OrderedDict
d['a'] = 1
d['b'] = 2
d['c'] = 3
 
print(d)
$ python main.py
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

ChainMap

The ChainMap class in Python is used to combine multiple dictionaries or mappings. The ChainMap class is a part of the collections module.

from collections import ChainMap
 
# Define two dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
 
# Create a ChainMap
chain = ChainMap(dict1, dict2)
 
print(chain)
$ python main.py
ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4})

Conclusion

In this article, we discussed the Python Collections Module and its various classes like Counter, defaultdict, deque, namedtuple, OrderedDict, and ChainMap. These classes provide alternatives to Python's built-in data types and offer more features and functionality.

References

Challenge Yourself

Easy Challenge

  1. Create a Counter object to count the number of characters in a string.
  2. Create a defaultdict object to store the frequency of words in a list.
  3. Create a deque object and perform various operations like append, appendleft, pop, popleft.

Medium Challenge

  1. Create a namedtuple to store the details of a student like name, roll number, and marks.
  2. Create an OrderedDict to store the details of students in a class and display the details in the order of roll numbers.
  3. Create a ChainMap to combine two dictionaries and access the values using keys.

Hard Challenge

  1. Create a program that uses all the classes from the Python Collections Module to perform various operations like counting the number of elements, storing default values, creating a double-ended queue, creating a tuple with named fields, creating a dictionary that remembers the order of keys, and combining multiple dictionaries.