Concepts / Working with Dictionaries

Working with Dictionaries

The dictionaries that you will be using are instances/objects of the dict class.

  • Programming

A Familiar Pattern

You may already have encountered dictionaries without naming them. When you use keyword arguments in a function, you specify names together with their corresponding values. The same key-value pattern appears when working with dictionaries.

A dictionary is an object made from the dict class, and its information is organized as key-value pairs.

Keys and Values

Think of a dictionary as a set of connections. Each key is associated with a value. The key-value pair is the basic unit that gives the dictionary its structure: the key identifies one entry, and the value is the information connected to that key.

containsmaps tocontainsmaps todictionarykey-value pairsnamekeyAdavaluescorekey95value
What does a dictionary contain, and how does each key connect to its corresponding value?

Tracing One Dictionary

Following Two Key-Value Pairs

Imagine a dictionary organized around two entries: the key name is connected to the value Ada, and the key score is connected to the value 95.

Start with the dictionary: The complete object is a dictionary containing key-value pairs.

Trace the first pair: Follow the key name to its associated value Ada.

Trace the second pair: Follow the key score to its associated value 95.

Compare the pairs: The two keys identify two separate connections inside the same dictionary object.

The dictionary can be understood by tracing each key to the value associated with it.

This example illustrates the internal idea without depending on a particular piece of program syntax. The important point is the relationship: a key-value pair is specified together, and the dictionary is the object that contains these relationships.

The dict Class Connection

The dictionaries you work with are instances, or objects, of the dict class. The class is therefore part of the explanation of what a dictionary is: an individual dictionary is an object created from that class.

class ofclass ofdictclassstudent_recorddictionary objectsettingsdictionary object
How is the dict class related to the individual dictionary objects created from it?

Order of Pairs

Since Python 3.7, the order of dictionary key-value pairs is the same as their input order. This means dictionaries are ordered structures in current Python versions described by the source material.

Reading Input Order

Consider a dictionary whose pairs are introduced in this order: name, score, then level.

Record the first pair: The name pair is introduced first.

Record the second pair: The score pair is introduced second.

Record the third pair: The level pair is introduced third.

Trace the resulting order: Since Python 3.7, the dictionary keeps the same order in which those pairs were input.

The pair order is name, score, level because that is the input order.

When order matters to your understanding of a dictionary, distinguish the order in which pairs were input from the key-value relationship itself. The dictionary has both an association between keys and values and, since Python 3.7, an input order for those pairs.

Functions and Symbol Tables

Keyword arguments provide a useful connection to dictionaries. In a function definition, the parameter list specifies key-value pairs through keyword arguments. When variables are accessed within the function, the source material compares that access to key access in a dictionary. In compiler design terminology, this dictionary is called the symbol table.

Mistakes to Avoid

  • Treating a dictionary as unrelated individual values rather than as key-value pairs.

    The key-value relationship is the defining organization described for dictionaries.

    Fix: Trace each key to its corresponding value and treat the pair as one unit.

  • Forgetting that an individual dictionary is an object of the dict class.

    The source identifies dictionaries as instances or objects of the dict class.

    Fix: Use dict for the class and dictionary object for an individual instance.

  • Assuming that dictionary pairs have no order in current Python.

    Since Python 3.7, dictionary pairs retain their input order.

    Fix: Remember that dictionaries are ordered structures with respect to input order in Python 3.7 and later.

  • Missing the connection between keyword arguments and dictionary-style organization.

    The source explains keyword arguments using the key-value-pair idea.

    Fix: Recognize the parameter name as the key-like part and its supplied value as the value-like part of the relationship.

Practice the Mental Model

EASY

A dictionary receives three key-value pairs in this order: title, author, year. Explain what each key is connected to, identify the class associated with the dictionary object, and state what order the pairs have in Python 3.7 or later.

Hints
  • Name the value associated with each key using the pair information in the prompt.
  • An individual dictionary is an instance of the dict class.
  • Use the input order when describing the order of the pairs.

What do you think happens?

A dictionary receives the pairs title, author, year in that order. What order should you expect for those pairs in Python 3.7 or later?

  • year, author, title
  • title, author, year
  • The source material says that no order is retained
Reveal answer

Answer: title, author, year

Since Python 3.7, the order of key-value pairs is the same as their input order.

Key Takeaways

  1. A dictionary is organized as key-value pairs.
  2. Each key connects to its corresponding value.
  3. Individual dictionaries are objects, or instances, of the dict class.
  4. Keyword arguments provide a familiar example of key-value organization.
  5. Since Python 3.7, dictionary pairs retain their input order.

Key Takeaways

  • Dictionaries organize information as key-value pairs.
  • A key identifies the value associated with it.
  • The dictionaries you use are instances of the dict class.
  • Keyword arguments connect familiar function syntax with dictionary-style key-value organization.
  • Since Python 3.7, dictionary key-value pairs retain their input order.