Modifying and Deleting Dictionary Items
Dictionaries are created empty using dict() or {} and store key-value pairs.
From Empty to Populated
A dictionary begins as a container for key-value pairs. It can be created empty with dict() or with curly brackets, {}. Once the empty dictionary exists, square bracket assignment connects a key with a value. The dictionary then contains the pair that was assigned.
Creating the Container
The two empty-dictionary forms are dict() and {}. They use different notation, but both create an empty dictionary. This gives you two equivalent starting points before any key-value pairs are added.
| Syntax | Starting contents | Purpose |
|---|---|---|
| dict() | No key-value pairs | Create an empty dictionary |
| {} | No key-value pairs | Create an empty dictionary |
After creation, square bracket assignment uses the form dictionary[key] = value. The key appears inside square brackets, and the value appears to the right of the assignment symbol. For example, starting with an empty dictionary named settings, the assignment settings['theme'] = 'dark' adds the key-value pair theme and dark.
Tracing Several Assignments
Building a Small Dictionary
Start with an empty dictionary and add the pairs name and Mira, level and 3, and active and True.
Start: Use either dict() or {} to create the empty dictionary.
First assignment: Assign Mira to the key name. The dictionary now contains one key-value pair.
Second assignment: Assign 3 to the key level. A second key-value pair is added.
Third assignment: Assign True to the key active. A third key-value pair is added.
The resulting dictionary is {'name': 'Mira', 'level': 3, 'active': True}.
The example shows the state changing one assignment at a time. Each square bracket expression identifies the key whose value is being assigned. The resulting dictionary contains the key-value pairs that were added.
Insertion Order and Display
In Python 3.7 and later, dictionaries preserve insertion order. This means that items appear in the sequence in which they were added. If name is added first, level second, and active third, the dictionary's items appear in that same sequence.
A dictionary's output format shows key-value pairs inside curly brackets. Pairs are separated by commas, and each key is connected to its value with a colon. Because this displayed format is also valid input syntax, the shown structure can be used to create a dictionary with multiple items at once.
Reading Output as Input
Use the displayed dictionary {'city': 'Lima', 'visits': 2} as a model for creating a dictionary with two items at once.
Identify the container: The curly brackets show that the structure is a dictionary.
Identify each pair: city is connected to Lima, and visits is connected to 2.
Preserve the written sequence: The city pair appears first and the visits pair appears second.
The same key-value structure serves as both dictionary output and valid input syntax.
Mistakes to Avoid
Assuming dict() and {} create different kinds of empty containers.
The source material identifies both forms as ways to create an empty dictionary.
Fix:
Recognize dict() and {} as two supported syntaxes for the same starting purpose.Leaving out the square brackets when assigning a key.
The documented assignment form places the key inside square brackets before the assignment symbol.
Fix:
Use dictionary[key] = value when adding a key-value pair.Expecting dictionary items to appear in an arbitrary order.
Python 3.7 and later preserve insertion order.
Fix:
Track the order in which pairs were added.Treating displayed dictionary output as unrelated formatting.
The dictionary output format is also valid input syntax.
Fix:
Read the displayed form as a key-value structure that can create a dictionary with multiple items.Guessing deletion syntax from this lesson.
The supplied source material explains creation, assignment, order, and output/input format, but does not state a deletion operation.
Fix:
Use only deletion syntax provided by a separate, authoritative lesson.
A dictionary starts empty. Describe the state after adding the pair language and Python, followed by the pair version and 3.7. Which pair appears first, and which two syntaxes could have been used to create the empty dictionary?
Hints
- Use square bracket assignment for each pair.
- Use insertion order to determine which pair appears first.
- The two empty-dictionary forms are dict() and {}.
Key Takeaways
- dict() and {} both create an empty dictionary.
- Square bracket assignment uses dictionary[key] = value to add a key-value pair.
- Python 3.7 and later preserve the order in which dictionary items are inserted.
- A dictionary's displayed key-value format is also valid syntax for creating a dictionary with multiple items.
- The supplied source material does not define deletion syntax, so deletion should be learned from a separate documented operation.
Key Takeaways
- Create an empty dictionary with dict() or {}.
- Add key-value pairs through square bracket assignment.
- Rely on insertion order in Python 3.7 and later when interpreting item order.
- Recognize dictionary output as valid input syntax.
- Do not assume deletion syntax that the source material has not specified.