Dictionary Methods
Dictionaries are created empty using dict() or {} and store key-value pairs.
A Dictionary Before It Has Data
A dictionary begins as a container for key-value pairs. You can create that container empty, then add pairs one at a time. Python provides two forms for creating an empty dictionary: dict() and {}.
{}
{}Adding the First Pair
Square bracket assignment adds a key-value pair to a dictionary. The general form is dictionary[key] = value. The expression inside the brackets identifies the key, and the expression after the equals sign supplies the associated value.
profile = {} profile["name"] = "Mira" profile["level"] = 3 print(profile)
Reading the Dictionary State
A dictionary is organized as key-value pairs. In the displayed form, each key is followed by its associated value, and multiple pairs appear together inside curly brackets. This makes the displayed dictionary easy to read as a representation of the data that was entered.
Building a Dictionary Step by Step
Create a dictionary containing a color and a quantity by starting with an empty dictionary.
Start empty: Use {} to create a dictionary with no key-value pairs.
Add the color: Use inventory["color"] = "blue" to add the key color and its value blue.
Add the quantity: Use inventory["quantity"] = 4 to add the key quantity and its value 4.
Inspect the result: The dictionary display contains both pairs inside curly brackets.
{'color': 'blue', 'quantity': 4}
Insertion Order in Python 3.7 and Later
In Python 3.7 and later, dictionaries preserve insertion order. That means dictionary items appear in the sequence in which you added them. If one pair is added first and another pair is added second, the displayed dictionary keeps that order.
{'first': 'open', 'second': 'save', 'third': 'close'}From Input Syntax to Display
The format Python displays for a dictionary is also valid input syntax for creating a dictionary with multiple items at once. Curly brackets surround the pairs, and each pair is written in key-value form. This gives you two connected ways to understand dictionaries: the displayed form shows the structure, and the same structure can be entered as dictionary input.
Mistakes with Dictionary Construction
Using parentheses as though they were the empty-dictionary syntax.
The two empty-dictionary forms covered here are dict() and {}.
Fix:
Use items = dict() or items = {}.Leaving out the square brackets when adding a pair.
A dictionary key is specified inside square brackets in the assignment form.
Fix:
Write profile["name"] = "Mira".Expecting dictionary pairs to appear in a different order from the order of insertion in Python 3.7 and later.
Python 3.7 and later preserve insertion order, so second appears before first in the displayed sequence.
Fix:
Read the displayed order as the order in which the pairs were added.Treating the displayed dictionary format as unrelated to input syntax.
The dictionary representation shown by Python is also valid input syntax for creating multiple pairs at once.
Fix:
Use the displayed key-value structure as a model for writing a dictionary literal.
Practice the State Changes
Create an empty dictionary using curly brackets. Add the pair "language": "Python", then add the pair "version": 3.7. Write the dictionary you expect Python to display and preserve in order.
Hints
- Begin with settings = {}.
- Use square bracket assignment for each pair.
- Place the pair added first before the pair added second in the displayed dictionary.
What do you think happens?
What will Python display after these assignments? record = {} record["topic"] = "dictionaries" record["length"] = 1
Reveal answer
Answer: {"topic": "dictionaries", "length": 1}
In Python 3.7 and later, dictionary items appear in the sequence in which they were added. topic was added before length.
Dictionary Construction Checklist
- Create an empty dictionary with dict() or {}.
- Add a key-value pair with dictionary[key] = value.
- In Python 3.7 and later, dictionary items appear in insertion order.
- A dictionary's displayed key-value format is also valid input syntax for creating multiple pairs at once.
Key Takeaways
- dict() and {} both create empty dictionaries.
- Square bracket assignment adds key-value pairs.
- Python 3.7 and later preserve the order in which dictionary pairs are inserted.
- The dictionary format Python displays can also be used as input syntax for multiple key-value pairs.