Concepts / Dictionary Basics and Operations

Dictionary Basics and Operations

The important change is the disappearance of the selected key-value pair. The other pair remains because the deletion targets the specified key rather than the whole dictionary.

  • Programming

A Targeted Removal

Suppose a dictionary contains several key-value pairs, but one pair is no longer needed. The relevant operation is not a search for the value. Instead, you identify the dictionary and specify the key that identifies the pair to remove. Python's del statement then deletes that key-value pair.

What do you think happens?

A dictionary contains the key name with the value Ada. To delete that pair, do you need to specify Ada as well as name?

  • Yes, both the key and value are required

  • No, the key is enough

  • Only the value is required

Reveal answer
Answer:

No, the key is enough.

The deletion operation specifies the dictionary and the key inside the indexing operator. It does not require the value corresponding to that key.

Before and After Deletion

Consider the generated example dictionary {"name": "Ada", "role": "designer"}. The key name identifies the value Ada, and the key role identifies the value designer. If the name pair is deleted, the resulting dictionary keeps the role pair and no longer contains the name pair.

nameAdaroledesignerroledesignerbefore deletion
What changes in the dictionary before and after del removes the key name?
Key point

The important change is the disappearance of the selected key-value pair. The other pair remains because the deletion targets the specified key rather than the whole dictionary.

The Dictionary-Key Path

The deletion form has three parts: the del statement, the dictionary, and the indexing operator containing the key to remove. In conceptual form, it is del dictionary[key]. The dictionary identifies the collection being changed. The key inside the indexing operator identifies the exact key-value pair targeted for removal.

profiledictionarynameindexing keynameAdaindex withidentifies
How does the dictionary and the key inside the indexing operator identify the exact key-value pair to remove?
Definition

A lookup is a dictionary operation that takes a key and finds the corresponding value. Deletion uses the same key-centered way of identifying the pair, but the purpose is to remove the pair rather than simply find its value.

A Complete Trace

Worked example

Removing the status Pair

A generated dictionary is {"status": "active", "owner": "Mina"}. Identify the pair removed by del records["status"].

  1. 1
    Identify the dictionary

    records is the dictionary that will be changed.

  2. 2
    Read the key

    The key inside the indexing operator is status.

  3. 3
    Locate the pair

    The key status corresponds to the value active, so the targeted pair is status with active.

  4. 4
    Apply del

    The del statement removes that key-value pair. The value active does not need to be supplied separately.

  5. 5
    Inspect what remains

    The owner pair remains in the dictionary.

Result

The status-active pair is removed, leaving the owner-Mina pair.

Deletion targetWhat identifies itIs the associated value needed?
One key-value pairThe dictionary and its keyNo
The corresponding valueNot the deletion form described hereThe operation is key-based

Dictionary deletion is described as a key-based operation.

Mistakes to Avoid

  • Common mistake

    Trying to delete by writing the value instead of the key.

    What it looks like

    The pair is {"status": "active"}, but the deletion target is status, not active.

    Why it fails

    The deletion form identifies the pair through the key inside the indexing operator.

    Do this instead

    Use the dictionary and the key that identifies the pair.

  • Common mistake

    Assuming the entire dictionary is removed.

    What it looks like

    Deleting the status pair from {"status": "active", "owner": "Mina"}.

    Why it fails

    The operation targets one specified key-value pair.

    Do this instead

    Expect the other key-value pairs to remain.

  • Common mistake

    Believing that the corresponding value must be known first.

    What it looks like

    Trying to write both status and active as deletion inputs.

    Why it fails

    The source description explicitly states that the value corresponding to the key is not needed for this operation.

    Do this instead

    Identify the key and use the dictionary-key form with del.

Best practice

When tracing a deletion, say the operation aloud in this order: dictionary, key, targeted pair, remaining pairs. This keeps the key-based nature of the operation visible and prevents the value from being mistaken for the deletion selector.

Practice Check

EASY

A generated dictionary contains {"language": "Python", "level": "beginner", "format": "course"}. You want to remove the language key-value pair. State which key identifies the pair, whether you need to know the value, and which two pairs remain afterward.

Hints
  • The deletion target is identified by the key inside the indexing operator.
  • The value is not required for this operation.
  • Only the specified key-value pair is removed.

Expected reasoning: language identifies the pair, the value Python does not need to be supplied separately, and the level-beginner and format-course pairs remain.

Key Takeaways

  1. 1
    A dictionary contains key-value pairs that can be identified through their keys.
  2. 2
    A lookup takes a key and finds its corresponding value.
  3. 3
    The del statement removes a specified key-value pair.
  4. 4
    The deletion form uses the dictionary and the key inside the indexing operator.
  5. 5
    You do not need to know the value corresponding to the key in order to delete the pair.

At a glance

  • Dictionary deletion is a key-based operation.
  • The del statement receives the dictionary and the indexed key identifying the pair to remove.
  • Deleting one pair does not target the other pairs in the dictionary.
  • The associated value does not need to be known or supplied separately.

Related concepts

More concepts starting with D