All about Collections Module in Python
Last Updated on November 5, 2023 by Editorial Team
Author(s): Muttineni Sai Rohith
Originally published on Towards AI.
As we all know, python has its own regular data type heroes β List, Tuple, Dictionary, and infamous Sets. But along with this, the Justice League of Python has its Superman β named Collections. In this article, we are going to dive deep into the world of collections and understand the use cases and advantages of it.
The collections module in Python implements a set of specialized container datatypes, providing alternatives to Pythonβs built-in datatypes like list, tuple, set, and Dictionary.
Letβs understand the importance of each functionality in the collections module.
namedTuple
is a module in collections that provides us with an easy way to create a class for storing structured data without defining a full-fledged class.
Instead of accessing Tuples with indexes, they are converted into a class and we can access them based on each identifier's names instead of indexes.
In the example above, in line 2, we are creating a namedTuple by name β Employee which holds the name, id, and age of the Employee. This will create a class variable that can be used to store Employee records.
namedTuples can also be accessed using indexes, as shown in line 3 they can be easily converted to a dictionary and also support Tuple unpacking.
Counter
Counter, as the name suggests, is used for counting the occurrences of elements in the collections β list, tuples, and string. It returns a dictionary with a key as an element and a value as the count of occurrences of a particular element.
As shown in the example, Counter returned occurrences of each particular in the list in dictionary format. most_common(k) method of Counter returns k most repeated occurrence. We can also perform addition, subtraction, intersection, and union of Counters.
deque
usually referred to as a deck, is a short form of double-end-queue. It serves as an optimized list to perform insertion and deletion of elements from both ends easily with O(1) complexity.
As shown above, elements can be added on both ends easily by using the append and append-left methods.
similar to list, deque provides reverse, rotate operations and pop, pop left methods to remove elements from both ends.
ChainMap
is used to combine multiple dictionaries into a single view without copying the data. As the name suggests, the mappings(dictionaries) are organized into a chain, and we can access and search them in the same order. If a key exists in both the dictionaries, then the latest dictionary in the chain is always given preference.
defaultdict
is used when we want to create a dictionary with default values for missing keys. The difference between a dictionary and a defaultdict is when we use defaultdict and try to access a missing key instead of raising key error, it will return the specified default value.
Ordereddict
is a dictionary that remembers the order in which the elements are entered. when we try to access the OrderedDict it will return elements in the order in which they are inserted.
UserDict
UserDict is useful when we want to create a dictionary with custom behavior. Suppose we want to create a dictionary where values entered are uppercased automatically, then UserDict comes to your rescue.
Same applies for UserList and UserString.
Sharing colab notebook for code.
Happy Learningβ¦..
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI