Modifying Dictionaries: Adding and Updating Pairs
A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.
From Positions to Labels
Imagine storing a person's name, age, email, and phone number in a list. You might need to remember that one position represents the name, another represents the age, and another represents the email. A dictionary organizes the same information differently: it uses meaningful labels, called keys, to identify the values. This makes the relationship between a piece of data and its meaning easier to understand.
Keys and Values
A dictionary is a mapping between keys and values. A key is the label used to identify information, and a value is the information connected to that label. Together, one key and its associated value form a key-value pair. The key-value pair is the fundamental unit of a dictionary.
A key-value pair is one key together with the single value that the key identifies. Each key maps to exactly one value.
Adding and Updating State
Modifying a dictionary means changing its collection of key-value pairs. When a new key is introduced, a new relationship is added: that key becomes connected to its value. When an existing key is given a different value, the relationship for that key is updated. The key still identifies the same kind of information, but the value associated with it changes.
Tracking a Person's Information
A dictionary already organizes a person's name and age. Trace what happens when an email pair is added and the age value is updated.
Initial pairs: The dictionary contains the keys name and age. Each key identifies one associated value.
Add a pair: The key email is introduced and connected to the person's email value. This adds another key-value pair to the dictionary.
Update a pair: The existing key age is connected to a new age value. The key remains age, but the value mapped to it changes.
Retrieve by meaning: A reader can identify the requested information by its label, such as email or age, instead of remembering a numeric position.
Adding creates a new key-value relationship; updating changes the value associated with an existing key.
Why Mappings Matter
Lists are accessed with integer positions. For example, a list lookup using position 0 retrieves the first item. Dictionaries are accessed with keys, so the lookup directly expresses what information is wanted. A dictionary can use keys such as strings, numbers, or tuples, whereas list indices must be integers. This allows the index itself to carry meaning.
Mistakes with Pairs
Treating a dictionary like a list
A dictionary is not a sequence indexed by position. Its lookup mechanism uses a key.
Fix:
Identify the meaningful label for the information and use that key to describe the lookup.Forgetting that a pair contains both parts
A key-value pair consists of a key together with its associated value.
Fix:
Describe the relationship as a pair: the key identifies the value it maps to.Assuming keys must be integers
Dictionary keys can be almost any type, including strings, numbers, and tuples.
Fix:
Choose a key that meaningfully identifies the value, such as name, age, student_id, or grade.Confusing an update with a new kind of index
Updating changes the value associated with an existing key; the key continues to identify the same kind of information.
Fix:
Separate the label from the current information connected to that label.
When organizing related information, choose keys that explain what their values represent. Labels such as student_id and grade communicate more than unexplained numeric positions, so a reader does not have to memorize what each position means.
Check Your Understanding
A record contains a person's name, age, and email. Explain which parts are keys, which parts are values, and what changes when an email pair is added after the name and age pairs already exist. Then explain what changes when the age value is updated.
Hints
- A key is the label used to identify a piece of information.
- A value is the information connected to that label.
- Adding an email pair creates another key-value relationship.
- Updating age changes the value associated with the existing age key.
What do you think happens?
A dictionary already has keys name and age. If the key email is added, does the existing age relationship disappear?
Reveal answer
Answer: No, the email relationship is added alongside the existing pairs.
A new key introduces another key-value pair. An update is different: it changes the value associated with an existing key.
Key Takeaways
- A dictionary maps keys to values rather than indexing values by list position.
- A key-value pair is the fundamental unit of a dictionary.
- Each key maps to exactly one value.
- Adding a pair introduces a new key and its associated value; updating changes the value connected to an existing key.
- Meaningful keys such as name, age, student_id, and grade make data easier to understand and retrieve.
Key Takeaways
- Dictionaries organize data as mappings between keys and values, not as sequences accessed by position.
- A key-value pair combines a meaningful key with the single value that key identifies.
- Adding a pair creates a new mapping, while updating changes the value associated with an existing key.
- Dictionary keys can be strings, numbers, tuples, and many other types, making them useful for non-integer labels.
- Using meaningful keys makes data retrieval clearer than relying on remembered list positions.