A dictionary finds related values through meaningful keys.
Lists organize values by position: position → value. A dictionary organizes one related group of information by meaningful names: key → value. For a student, student["name"] explains more clearly what we want than student[0] would.
student = {
"name": "Ana",
"age": 22,
"course": "Python",
}
print(student["name"])
print(student["course"])Curly braces {} hold comma-separated key: value pairs. Here "name" → "Ana", "age" → 22, and "course" → "Python". A key tells Python which related value to find. Read student["name"] as student → "name" → "Ana": "name" is a key, not a list position.
Ana
PythonA dictionary is mutable, like a list. Assignment with an existing key changes that value: before, "age" → 22; after student["age"] = 23, "age" → 23. The key stays the same while its associated value changes.
One assignment, two possible effects: When the key already exists, dictionary[key] = value updates its value. When the key is new, the same form adds a new key-value pair. A dictionary cannot keep two separate entries with the same key.
student = {"name": "Ana", "age": 22}
student["age"] = 23
student["city"] = "Novi Sad"
print(student["age"])
print(student["city"])The first assignment updates the existing "age" value. The second creates a new "city" → "Novi Sad" pair. The same bracket syntax is useful because the key tells Python whether it should update or add.
student = {"name": "Ana", "age": 22}
print("name" in student)
print("email" in student)key in dictionary checks whether that key exists. The output is True and then False. It does not search values, so "Ana" in student is not a check for the value "Ana".
student = {"name": "Ana", "age": 22, "course": "Python"}
print(len(student))
print(student.get("email"))
print(student.get("email", "Not provided"))len(student) counts three key-value pairs. student["email"] would raise KeyError because that key does not exist. get() is a safe lookup: it returns None when the key is absent, or the default "Not provided" when you supply one.
student = {"name": "Ana", "age": 22}
for key in student:
print(key, student[key])
for key, value in student.items():
print(key, value)A plain for key in student loop gives keys. Use student[key] to find each associated value. items() gives key-value pairs so Python can unpack them into the two variables key and value.
profile = {"name": "Ana", "points": 10}
profile["points"] = 15
profile["level"] = "beginner"
print(profile["points"])
print("level" in profile)
print(len(profile))Trace the state before running: start with two pairs, update "points", then add "level". The lookup prints 15, membership for "level" is True, and the dictionary now has three pairs.
Choose a collection by the problem it solves
- Use a list for ordered values that may changeChoose a list when order or positions matter and the collection may grow, shrink, or be updated.
- Use a tuple for one fixed related groupChoose a tuple for stable related values whose positions have meaning, such as coordinates.
- Use a set for uniqueness and membershipChoose a set when duplicates should disappear and membership matters more than position.
- Use a dictionary for named related informationChoose a dictionary when values should be found through meaningful keys such as
"name","email", or"course".
Common beginner mistakes
- Confusing a key with its valueIn
student["name"],"name"is the key and"Ana"is its value. The key helps Python find the value. - Forgetting quotes around a string key
student[name]looks for a variable calledname. Writestudent["name"]when the key itself is the string"name". - Accessing a missing key without a plan
student["email"]raisesKeyErrorwhen no"email"key exists. Useget()when a missing key is an expected possibility. - Thinking in checks values
"name" in studentchecks keys. Do not assume thatinautomatically searches dictionary values. - Expecting duplicate keysAssigning a value to an existing key replaces that key’s value; it does not create a second independent copy of the key.
