Skip to content

mapping

In Python, a mapping is a data structure that associates keys with values. The most common example of a mapping in Python is the built-in dict data type, which allows you to store and retrieve data by using unique keys.

Python defines a mapping as a container object that supports arbitrary key lookups and implements the methods specified in the collections.abc.Mapping or collections.abc.MutableMapping abstract base classes. Besides dict, the standard library also includes collections.defaultdict, collections.OrderedDict, and collections.Counter.

Mappings are a powerful tool when you need to efficiently look up data based on a unique identifier. In Python, the dictionary is implemented as a hash table, which provides lookups, insertions, and deletions in constant time, O(1), on average.

Example

Here’s an example of how to create and use a dictionary:

Language: Python
>>> ages = {
...     "Alice": 28,
...     "Bob": 34,
...     "Charlie": 22
... }

>>> ages["Alice"]
28
>>> ages["David"] = 30
>>> ages
{'Alice': 28, 'Bob': 34, 'Charlie': 22, 'David': 30}
>>> ages["Alice"] = 29
>>> ages
{'Alice': 29, 'Bob': 34, 'Charlie': 22, 'David': 30}

In this example, you use a Python dictionary to store and manipulate key-value pairs. You can access values by their keys, add new pairs, or update existing values.

Python Mappings: A Comprehensive Guide

Tutorial

Python Mappings: A Comprehensive Guide

In this tutorial, you'll learn the basic characteristics and operations of Python mappings. You'll explore the abstract base classes Mapping and MutableMapping and create a custom mapping.

intermediate python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Sept. 21, 2026