Concepts / Creating and Modifying Dictionaries

Creating and Modifying Dictionaries

Dictionaries are also used to build histograms, which are collections of tallies.

  • Programming

A Dictionary as a Set of Associations

A dictionary is a collection of associations. Each association connects something used to look up data with the data itself. The lookup part is called a key, and the data connected to it is called a value. Together, the key and value form a key-value pair, also called an item.

Key point

A key identifies an association, a value is the data associated with that key, and a key-value pair is the complete combination of both.

DictionaryKey-value pairitemKeyunique identifierValueassociated datacontainshashas
What contains what in a dictionary, and how does each unique key connect to its value?

Keys, Values, and Items

Keys and values always work together in a dictionary. A key without a value does not form a complete key-value pair. A value without a key has no lookup part connecting it to the dictionary association. The complete pairing is called an item in everyday Python terminology.

Worked example

Identifying the Parts of an Association

Consider the generated dictionary association name connected to Ada. Identify the key, the value, and the complete item.

  1. 1
    Find the key

    The key is name because it is the part used to identify or look up the association.

  2. 2
    Find the value

    The value is Ada because it is the data connected to the key.

  3. 3
    Name the complete item

    The combination of name and Ada is the key-value pair, also called an item.

Result

Key: name. Value: Ada. Complete item: the key-value pair connecting name with Ada.

From Key to Stored Value

A lookup operation takes a key and returns its corresponding value. For example, when you work with an expression such as my_dict['name'], you are performing a lookup using the key name. The important idea is that the operation begins with a key and produces the associated value.

Keylookup inputHash functionkey conversionPositioncalculated locationValuecorresponding dataentersmaps tolocates
How does a key move through a hash function to locate its value efficiently?

Python implements dictionaries with a hashtable. A hashtable uses a hash function to convert each key into a numerical position. That position indicates where the key's associated data should be stored or retrieved. Instead of searching through every entry, the hashtable uses the calculated position to locate the requested data efficiently.

Definition

Implementation means the specific method chosen to accomplish a task. In this topic, the hashtable is the implementation Python uses for dictionaries.

Operations and Their Implementation

When you describe dictionary behavior, separate the operation from the implementation. A lookup is the operation: it takes a key and returns the corresponding value. The hashtable is the implementation: it uses a hash function to map the key to a position so the value can be located efficiently.

Lookup operationrequest a valueKeylookup inputHashtabledictionary implementationHash functionmaps keyValuelookup resultusesentersuseslocates
What happens inside the dictionary when a lookup is performed?
Key point

Fast dictionary behavior is explained by the relationship between the lookup operation and the hashtable implementation. The operation describes what you ask for; the implementation describes how Python finds the associated data.

Tracking a Dictionary Change

Creating or modifying a dictionary changes its collection of associations. Conceptually, adding a new key-value pair gives the dictionary another association. Modifying an existing entry changes the data associated with a key while preserving the idea that the key identifies the association. In either case, describe the result using the terms key, value, and key-value pair.

Dictionaryname: AdanameAdaDictionaryname: Ada; role: learnerrolelearnercontainscontains
How does a dictionary change before and after assigning a new key-value pair?
Worked example

Describing the Change

A dictionary first contains the generated pair name connected to Ada. A second pair role connected to learner is then added. Describe the dictionary after the change.

  1. 1
    Identify the original item

    The original item has name as its key and Ada as its value.

  2. 2
    Identify the added item

    The added item has role as its key and learner as its value.

  3. 3
    Describe the result

    The dictionary now contains both key-value pairs. The new association does not replace the original association because it uses a different key.

Result

The dictionary contains the items name-to-Ada and role-to-learner.

Mistakes in Dictionary Language

  • Common mistake

    Calling the value the key

    What it looks like

    Describing Ada as the lookup identifier in the generated name-to-Ada association.

    Why it fails

    The key is the identifier used to look up data, while the value is the data connected to that identifier.

    Do this instead

    Call name the key and Ada the value.

  • Common mistake

    Using item to mean only the value

    What it looks like

    Saying that Ada alone is the item.

    Why it fails

    An item is another name for the complete key-value pair.

    Do this instead

    Use item for the combination of name and Ada.

  • Common mistake

    Confusing an operation with an implementation

    What it looks like

    Calling the hashtable the lookup operation.

    Why it fails

    A lookup is what the dictionary does when it takes a key and returns its value. The hashtable is the method Python uses to implement dictionaries.

    Do this instead

    Describe lookup as the operation and hashtable as the implementation.

  • Common mistake

    Describing a lookup as a search through every entry

    What it looks like

    Assuming that Python must inspect every dictionary entry before returning a value.

    Why it fails

    The source explains that a hashtable uses a hash function to calculate a position for the key, enabling fast lookup.

    Do this instead

    Explain that the key is mapped to a position used to locate the associated value.

Edge case

When a dictionary-related discussion involves many associations, keep the vocabulary consistent. A dictionary contains items; each item contains a key and a value; a lookup takes a key and returns its corresponding value. This terminology remains useful even when the dictionary contains thousands of entries.

Practice and Transfer

EASY

A generated dictionary contains the associations subject-to-programming and level-beginner. Identify every key, every value, and every key-value pair. Then explain what a lookup using subject would return and describe the role of the hashtable in that lookup.

Hints
  • Separate the lookup identifiers from the data connected to them.
  • Each complete association is one item or key-value pair.
  • A lookup begins with the key and returns its corresponding value.
  • The hashtable uses a hash function to map the key to a position.

For a second practice task, explain the difference between these statements: a dictionary performs a lookup, and a dictionary is implemented with a hashtable. The first statement names the operation. The second names the method used to accomplish it.

Terminology Summary

  1. 1
    A key is a unique identifier used to look up associated data.
  2. 2
    A value is the data connected to a key.
  3. 3
    A key-value pair, also called an item, contains both the key and the value.
  4. 4
    A lookup takes a key and returns its corresponding value.
  5. 5
    A hashtable is Python's dictionary implementation; its hash function maps keys to positions so values can be located efficiently.

At a glance

  • Keys identify dictionary associations, values are the associated data, and key-value pairs are complete items.
  • A lookup begins with a key and returns its corresponding value.
  • Python implements dictionaries with hashtables.
  • A hash function maps a key to a numerical position, helping the hashtable locate data efficiently.
  • Use operation to describe what the dictionary does and implementation to describe how Python accomplishes it.

Related concepts

More concepts starting with C