Introduction to Python’s Collections Module

python_tutorials

Introduction

Collections in Python 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 was introduced to improve the functionalities of the built-in collection containers. Python collections module was first introduced in its 2.4 release. This tutorial is based on its latest stable release (3.7 version).

Collections Module

In this tutorial we will discuss 6 of the most commonly used data structures from the Python collections module. They are as follows:

The Counter

Counter is a subclass of dictionary object. The Counter() function in collections module takes an iterable or a mapping as the argument and returns a Dictionary. In this dictionary, a key is an element in the iterable or the mapping and value is the number of times that element exists in the iterable or the mapping.

You have to import the Counter class before you can create a counter instance.

from collections import Counter
Create Counter Objects

There are multiple

To finish reading, please visit source site