Dictionaries
Model a record with named fields instead of unexplained numeric positions.
On this page
The concept
A dictionary maps unique keys to values. Keys must be hashable, and strings are common because they label fields clearly. student["name"] expresses a field access; it is not a positional lookup. Assigning an existing key replaces its value, while assigning a new key adds a field.
Use square brackets for required fields whose absence should fail. Use get when absence is expected and there is a meaningful default. in checks keys. Modern Python dictionaries preserve insertion order, but that does not mean their values are automatically sorted.
Worked example
student = {"name": "Avan", "age": 20, "city": "Sulaymaniyah"}
print(student["name"])
print(student.get("email", "Not provided"))
student["age"] = 21
student["active"] = True
print("name" in student)
print(list(student.keys()))
print(list(student.values()))
print(student)Trace the result
- The required name is retrieved directly.
- The missing email uses a display default instead of raising KeyError.
- The age is updated, active is added, and keys/values are shown separately.
Avan
Not provided
True
['name', 'age', 'city', 'active']
['Avan', 21, 'Sulaymaniyah', True]
{'name': 'Avan', 'age': 21, 'city': 'Sulaymaniyah', 'active': True}Line-by-line walkthrough
Line 1: student = {"name": "Avan", "age": 20, "city": "Sulaymaniyah"}
Evaluate {'name': 'Avan', 'age': 20, 'city': 'Sulaymaniyah'}, then bind or update student with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 2: print(student["name"])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 3: print(student.get("email", "Not provided"))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 4: student["age"] = 21
Evaluate 21, then bind or update student['age'] with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 5: student["active"] = True
Evaluate True, then bind or update student['active'] with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 6: print("name" in student)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: print(list(student.keys()))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: print(list(student.values()))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 9: print(student)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Checking "Sara" in student would search keys rather than values. Be explicit about what kind of membership you need.