Concepts / Modifying Dictionaries: Adding and Updating Pairs

Modifying Dictionaries: Adding and Updating Pairs

A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.

  • Programming

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.

positionkeyListinteger position0nameDictionarymeaningful keynameperson's name
How does a dictionary locate a value by its key instead of by an integer position like a list?

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.

maps tomaps tomaps tonamekeya person's namevalueagekeya person's agevalueemailkeya person's emailvalue
Which value is connected to each key, and how does following a key lead to its associated value?

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.

existing pairsexisting pairsadditional pairnameperson's namenameperson's nameageearlier age valueageupdated age valueemailperson's email
What changes in a dictionary when a new key-value pair is added or an existing key's value is updated?

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.

position selectsposition selectskey maps tokey maps to0first itemnameperson's namelist valuemapped value1second itemageperson's age
How does a dictionary locate a value by its key instead of by an integer position like a list?
access by positionaccess by keyaccess by keyListinteger indicesstudent ID positionmust be rememberedDictionarymeaningful keysstudent_ididentifies the datagradeidentifies the data
How can meaningful labels such as names or categories be used to access data instead of numeric positions?

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

EASY

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?

  • Yes, adding one key replaces all existing pairs
  • No, the email relationship is added alongside the existing pairs
  • There is no way to tell because dictionaries use positions
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

  1. A dictionary maps keys to values rather than indexing values by list position.
  2. A key-value pair is the fundamental unit of a dictionary.
  3. Each key maps to exactly one value.
  4. Adding a pair introduces a new key and its associated value; updating changes the value connected to an existing key.
  5. 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.