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.

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 efficient lookups, insertions, and deletions in constant time, O(1).

Example

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

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.

Tutorial

Dictionaries in Python

Learn how dictionaries in Python work: create and modify key-value pairs using dict literals, the dict() constructor, built-in methods, and operators.

basics python

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


By Leodanis Pozo Ramos • Updated Sept. 25, 2025