Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de Understanding the association with dictionaries in Python: Mapping and MutableMapping - Interactive Tutorial
Python

Understanding the association with dictionaries in Python: Mapping and MutableMapping - Interactive Tutorial

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 2 janvier 2025 · 7 min of reading

In the fascinating world of Python programming, mappings play a crucial role as fundamental data structures. Whether for organizing, accessing, or manipulating data, mastering mappings is essential for any developer looking to optimize their applications. This article will guide you through the subtleties of the abstract classes mapping and mutablemapping, while showing you how to create your own custom mappings. Get ready to dive into a universe where data flexibility and efficiency are limited only by your imagination.

Understanding the main characteristics of mappings

Mappings in Python, often called dictionaries, are essential data structures that allow you to store and manage key-value pairs. Understanding their main characteristics is crucial to leverage their power and flexibility in your programs.

Basic structure of a dictionary

A dictionary in Python is defined by curly braces and key-value pairs are separated by colons :. Here is a simple example:
python
In this example, "name", "age", and "profession" are keys, and "Alice", 30, and "engineer" are the associated values.

Accessing and modifying values

The values in a dictionary can be easily accessed and modified using the keys. For example:
python
If you try to access a key that does not exist, Python raises a KeyError exception. To avoid this, you can use the get() method which returns None (or a default value you can specify) if the key does not exist:
python

Adding and removing items

Adding a new key-value pair is as simple as assigning it:
python
To remove a key-value pair, you can use the del method or the pop() method:
python

Iterating over a dictionary

Dictionaries allow for easy iteration over their keys, values, or key-value pairs:
python

Key properties

Keys in a dictionary must be immutable and unique. This means you can use types like strings, numbers, or tuples (as long as the elements of the tuple are themselves immutable) as keys, but not lists or dictionaries.
Mappings in Python are a very efficient data structure for searching, inserting, and deleting items, making dictionaries particularly useful for a variety of programming tasks.

Exploring the abstract base classes mapping and mutablemapping

The abstract base classes Mapping and MutableMapping are part of the collections.abc library in Python and define a standard for objects that behave like dictionaries. These classes provide a robust foundation for creating custom mapping objects.

Understanding the Mapping class

The Mapping class is an abstract class that defines the necessary methods for an object to be considered a mapping. It requires the implementation of certain fundamental methods such as __getitem__, __iter__, and __len__. These methods allow reading elements from the mapping, iterating over them, and calculating their count.
Here's a simple example of how you might start implementing a class using Mapping:
python

Exploring the MutableMapping class

MutableMapping extends Mapping by adding methods that allow modifying elements, such as __setitem__, __delitem__, and pop. This makes objects derived from MutableMapping not only readable but also modifiable.
Here is a basic implementation example of MutableMapping:
python
These abstract classes allow you to create custom mappings that adhere to the Python dictionary API conventions while providing you the flexibility to add additional features. Whether you need to create a data structure with specific behavior or simply make slight modifications to a dictionary, Mapping and MutableMapping provide a solid foundation to get started.

Creating a user-defined mapping

Creating a custom mapping allows you to design data structures tailored to specific needs while leveraging the flexibility of the abstract classes Mapping and MutableMapping. In this section, we will develop a user-defined mapping class that adds custom features to the basic structure of a dictionary.

Defining the basic structure

To create a custom mapping, we will start by extending MutableMapping, which will allow us to manage both read access and modifications of the data. Suppose we want a mapping that logs any changes made to the data.
python

Adding custom features

With JournalisedMapping, we have added a get_log() method that returns a log of changes. This feature is useful for tracking the history of changes, which can be crucial in certain applications, like state tracking in a financial computation application.
python

Benefits of custom mappings

Using user-defined mappings allows you to create data structures that not only store information but also provide advanced features such as data validation, logging, or even automatic calculations on each modification. This approach offers great flexibility to adapt Python's base structures to specific use cases, making your code more readable and maintainable.

Creating a user-defined mutable mapping

Creating a user-defined mutable mapping involves building a class that not only stores and retrieves data but also allows for appropriate modifications. This is particularly useful when you want to encapsulate complex behaviors not covered by standard Python dictionaries.

Extending MutableMapping

To create a mutable mapping, we need to extend the MutableMapping class and implement its abstract methods. Suppose we want to create a mapping that enforces a specific type for the values, for example, only integers.
python

Additional features

The IntegerOnlyMapping class only accepts integers as values and raises a ValueError if another type is attempted to be inserted. This ensures the integrity of the stored data.
python

Benefits of custom mutable mappings

By defining a custom mutable mapping, you can integrate rules and constraints directly into the data structure. This simplifies data management and enhances the security and reliability of your application. Moreover, it allows centralizing the data management logic, making the code easier to understand and maintain.

Inheriting from dict and collections.UserDict

Inheriting from dict or collections.UserDict offers different approaches to creating custom mappings in Python. Each of these options has specific advantages depending on your application's needs.

Inheriting from dict

Inheriting from the built-in dict class is a straightforward method to add extra functionalities to standard dictionaries. This allows maintaining the efficiency of basic operations while adding methods or overriding existing ones. Here is a simple example where we override the __setitem__ method to track the number of changes made to the dictionary:
python
This example demonstrates how you can easily extend the functionality of a dictionary while preserving its native behavior.

Using collections.UserDict

collections.UserDict is a wrapper class around a dictionary that makes inheritance easier, especially when you need to override base behaviors. Unlike inheriting directly from dict, UserDict stores data in a separate dictionary instance called data, making customization more intuitive.
Here’s how you might use it to implement a logging function:
python

Choosing the right approach

Choosing between dict and UserDict often depends on the complexity of the logic you need to implement. For minor modifications, inheriting from dict is often sufficient. However, for more complex behaviors, UserDict provides a clearer and easier-to-manage interface.

Conclusion

By exploring the different ways to create and customize mappings in Python, we have discovered the versatility and power these data structures offer to developers. Whether using the abstract classes Mapping and MutableMapping to define specific behaviors, or inheriting from dict and UserDict to extend existing functionalities, Python provides a plethora of tools suited for various needs.

Practical application

These techniques allow the creation of custom mappings that can validate data, track changes, or integrate advanced features like logging. By choosing the right approach, you can not only ensure the integrity and efficiency of your data but also enhance the readability and maintainability of your code.

Towards new possibilities

By incorporating these concepts into your projects, you can more effectively meet the specific requirements of your applications, whether it’s managing complex configurations, processing large datasets, or ensuring change tracking in critical systems. The flexibility of Python mappings will allow you to design robust solutions tailored to a wide range of scenarios.
In summary, mastering mappings in Python enriches your development toolbox and opens the door to more creative and efficient programming.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee