Concepts / Modifying Dictionaries: Adding and Updating Pairs

Modifying Dictionaries: Adding and Updating Pairs

The key-value pair is the fundamental unit of a dictionary.

  • 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.

Listinteger position0nameDictionarymeaningful keynameperson's namepositionkey
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.

namekeya person's namevalueagekeya person's agevalueemailkeya person's emailvaluemaps tomaps tomaps to
Which value is connected to each key, and how does following a key lead to its associated value?
Definition

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.

Worked example

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.

  1. 1
    Initial pairs

    The dictionary contains the keys name and age. Each key identifies one associated value.

  2. 2
    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.

  3. 3
    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.

  4. 4
    Retrieve by meaning

    A reader can identify the requested information by its label, such as email or age, instead of remembering a numeric position.

Result

Adding creates a new key-value relationship; updating changes the value associated with an existing key.

nameperson's namenameperson's nameageearlier age valueageupdated age valueemailperson's emailexisting pairsexisting pairsadditional pair
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.

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

Mistakes with Pairs

  • Common mistake

    Treating a dictionary like a list

    What it looks like

    Thinking of the requested information as being at position 0 or position 1

    Why it fails

    A dictionary is not a sequence indexed by position. Its lookup mechanism uses a key.

    Do this instead

    Identify the meaningful label for the information and use that key to describe the lookup.

  • Common mistake

    Forgetting that a pair contains both parts

    What it looks like

    Talking about a key without identifying the value connected to it

    Why it fails

    A key-value pair consists of a key together with its associated value.

    Do this instead

    Describe the relationship as a pair: the key identifies the value it maps to.

  • Common mistake

    Assuming keys must be integers

    What it looks like

    Assuming that only numeric labels can identify dictionary data

    Why it fails

    Dictionary keys can be almost any type, including strings, numbers, and tuples.

    Do this instead

    Choose a key that meaningfully identifies the value, such as name, age, student_id, or grade.

  • Common mistake

    Confusing an update with a new kind of index

    What it looks like

    Thinking that changing a value changes the meaning of the key

    Why it fails

    Updating changes the value associated with an existing key; the key continues to identify the same kind of information.

    Do this instead

    Separate the label from the current information connected to that label.

Best practice

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. 1
    A dictionary maps keys to values rather than indexing values by list position.
  2. 2
    A key-value pair is the fundamental unit of a dictionary.
  3. 3
    Each key maps to exactly one value.
  4. 4
    Adding a pair introduces a new key and its associated value; updating changes the value connected to an existing key.
  5. 5
    Meaningful keys such as name, age, student_id, and grade make data easier to understand and retrieve.

At a glance

  • 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.

Related concepts

More concepts starting with M