A mutable mapping in Python is a container that stores data in key-value pairs and can be modified after it is created.
In Python, mappings are collections where each unique key is associated with a specific value, allowing you to retrieve the value using its key. The "mutable" aspect means that you can change the contents of the mapping in place after it has been initialized.
According to the provided reference, the concept of MutableMapping
in Python's collections.abc
module formalizes this by extending the base Mapping
type. It adds methods for modifying the container.
Key Characteristics and Modification Methods
Mutable mappings provide methods and operations that allow you to:
- Add New Items: Introduce new key-value pairs.
- Change Existing Items: Update the value associated with an existing key.
- Remove Items: Delete key-value pairs from the mapping.
The reference highlights specific methods enabling this mutability, including:
__setitem__
: Used to assign or change a value for a given key (e.g.,my_map[key] = value
).__delitem__
: Used to remove an item based on its key (e.g.,del my_map[key]
).update
: Used to add items from another mapping or an iterable of key-value pairs.
These methods are part of the definition of what makes a mapping "mutable".
Python's Primary Mutable Mapping: dict
The most common and fundamental example of a mutable mapping in Python is the built-in dictionary (dict
).
Let's look at a simple example using a dictionary:
# Create an empty mutable mapping (a dictionary)
my_mutable_map = {}
print(f"Initial map: {my_mutable_map}")
# Add items (using __setitem__ implicitly)
my_mutable_map['apple'] = 1
my_mutable_map['banana'] = 2
print(f"After adding items: {my_mutable_map}")
# Change an existing item (using __setitem__ implicitly)
my_mutable_map['apple'] = 10
print(f"After changing 'apple': {my_mutable_map}")
# Remove an item (using __delitem__ implicitly)
del my_mutable_map['banana']
print(f"After removing 'banana': {my_mutable_map}")
# Add multiple items or update existing ones (using update)
my_mutable_map.update({'orange': 3, 'grape': 4, 'apple': 15})
print(f"After updating: {my_mutable_map}")
This example clearly demonstrates how the my_mutable_map
(a dictionary) changes over time, illustrating its mutable nature.
Mutable vs. Immutable Mappings
While mutable mappings like dict
are the standard, the concept of an immutable mapping exists (though no built-in type directly implements only Mapping
without mutation methods in the standard library for common use). An immutable mapping, in contrast, would not allow any additions, deletions, or modifications to its contents after creation.
Comparison Summary:
Feature | Mutable Mapping (e.g., dict ) |
Immutable Mapping (Conceptual) |
---|---|---|
Can change size? | Yes | No |
Can change items? | Yes | No |
Key Methods | __setitem__ , __delitem__ , update , etc. |
Access methods only (__getitem__ , keys , values , etc.) |
Primary Example | dict |
None commonly used built-in |
In summary, a mutable mapping is a collection of key-value pairs that offers methods to alter its contents after creation, with Python's built-in dict
being the prime example.