Working with Dictionaries
The dictionaries you work with are instances, or objects, of the dict class.
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.
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.
- 1Start with the dictionary
The complete object is a dictionary containing key-value pairs.
- 2Trace the first pair
Follow the key name to its associated value Ada.
- 3Trace the second pair
Follow the key score to its associated value 95.
- 4Compare 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.
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.
- 1Record the first pair
The name pair is introduced first.
- 2Record the second pair
The score pair is introduced second.
- 3Record the third pair
The level pair is introduced third.
- 4Trace 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
Common mistake Treating a dictionary as unrelated individual values rather than as key-value pairs.
What it looks likeThinking of Ada and 95 as simply two values, without identifying the keys connected to them.
Why it failsThe key-value relationship is the defining organization described for dictionaries.
Do this insteadTrace each key to its corresponding value and treat the pair as one unit.
Common mistake Forgetting that an individual dictionary is an object of the dict class.
What it looks likeDescribing dict and a particular dictionary object as if they were the same thing.
Why it failsThe source identifies dictionaries as instances or objects of the dict class.
Do this insteadUse dict for the class and dictionary object for an individual instance.
Common mistake Assuming that dictionary pairs have no order in current Python.
What it looks likeIgnoring the order in which pairs were entered.
Why it failsSince Python 3.7, dictionary pairs retain their input order.
Do this insteadRemember that dictionaries are ordered structures with respect to input order in Python 3.7 and later.
Common mistake Missing the connection between keyword arguments and dictionary-style organization.
What it looks likeTreating keyword arguments as having no relationship to named values.
Why it failsThe source explains keyword arguments using the key-value-pair idea.
Do this insteadRecognize the parameter name as the key-like part and its supplied value as the value-like part of the relationship.
Practice the Mental Model
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?
Reveal answer
title, author, year
Since Python 3.7, the order of key-value pairs is the same as their input order.
Key Takeaways
- 1A dictionary is organized as key-value pairs.
- 2Each key connects to its corresponding value.
- 3Individual dictionaries are objects, or instances, of the dict class.
- 4Keyword arguments provide a familiar example of key-value organization.
- 5Since Python 3.7, dictionary pairs retain their input order.
At a glance
- 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.