Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

All about Collections Module in Python
Latest   Machine Learning

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.

Collections

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.

namedTuple

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.

namedTuple — 2

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.

Counter

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.

deque

As shown above, elements can be added on both ends easily by using the append and append-left methods.

deque — 2

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.

ChainMap

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.

with dictionary
defaultdict

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.

OrderedDict

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.

UserDict

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

Feedback ↓