Concepts / Creating and Modifying Dictionaries

Creating and Modifying Dictionaries

Keys and values are the two components of a key-value pair (item). Keys are unique identifiers; values are the data they point to.

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

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.

containshashasDictionaryKey-value pairitemKeyunique identifierValueassociated data
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.

Identifying the Parts of an Association

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

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

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

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

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.

entersmaps tolocatesKeylookup inputHash functionkey conversionPositioncalculated locationValuecorresponding data
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.

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.

usesentersuseslocatesLookup operationrequest a valueKeylookup inputHashtabledictionary implementationHash functionmaps keyValuelookup result
What happens inside the dictionary when a lookup is performed?

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.

containscontainsDictionaryname: AdanameAdaDictionaryname: Ada; role: learnerrolelearner
How does a dictionary change before and after assigning a new key-value pair?

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.

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

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

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.

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

Mistakes in Dictionary Language

  • Calling the value the key

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

    Fix: Call name the key and Ada the value.

  • Using item to mean only the value

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

    Fix: Use item for the combination of name and Ada.

  • Confusing an operation with an implementation

    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.

    Fix: Describe lookup as the operation and hashtable as the implementation.

  • Describing a lookup as a search through every entry

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

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

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

Key Takeaways

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