Python Dictionary Processing Example
This example shows how to process data stored in a Python dictionary.
You will use a small dictionary of student scores and learn how to:
- read values by key
- update an existing value
- add a new key-value pair
- loop through all entries
- calculate a simple result from the data
This is not a full dictionary tutorial. It is one practical example that helps you see how dictionaries work in a small script. If you want the full concept, see Python dictionaries explained.
What this example shows
In this example, you will:
- Use a dictionary to store related data as key-value pairs
- Read values by key
- Change existing values
- Add new entries
- Loop through all entries
- Calculate a simple result from dictionary data
Example problem
Suppose you have student names and test scores.
You want to:
- start with a small student score dictionary
- update one student's score
- add a new student
- print all names and scores
- find the highest score
Here is the complete example:
student_scores = {
"Alice": 85,
"Bob": 92,
"Cara": 78
}
# update a value
student_scores["Alice"] = 88
# add a new key-value pair
student_scores["Dan"] = 95
# loop through the dictionary
for name, score in student_scores.items():
print(name, score)
# simple summary
highest_name = max(student_scores, key=student_scores.get)
print("Top student:", highest_name)
print("Top score:", student_scores[highest_name])
Example output:
Alice 88
Bob 92
Cara 78
Dan 95
Top student: Dan
Top score: 95
Build the dictionary
A dictionary stores data as key-value pairs.
In this example:
- the keys are student names
- the values are student scores
Start with a small dictionary literal:
student_scores = {
"Alice": 85,
"Bob": 92,
"Cara": 78
}
This is a good beginner example because:
- the keys are strings
- the values are integers
- each name clearly matches one score
Access and update values
You can read a value by using its key inside square brackets.
student_scores = {
"Alice": 85,
"Bob": 92,
"Cara": 78
}
print(student_scores["Alice"])
Output:
85
To change a value, assign a new value to the same key:
student_scores["Alice"] = 88
print(student_scores["Alice"])
Output:
88
If you use square brackets with a key that does not exist, Python raises a KeyError.
print(student_scores["Eli"])
If the key may be missing, use get() instead:
print(student_scores.get("Eli"))
Output:
None
To learn more, see how to access values in a dictionary in Python and the Python dictionary get() method.
Add new data
To add a new item, assign a value to a new key:
student_scores["Dan"] = 95
print(student_scores)
Output:
{'Alice': 88, 'Bob': 92, 'Cara': 78, 'Dan': 95}
Dictionaries grow automatically. You do not need to create space first.
If you want more practice with this step, see how to add a key to a dictionary in Python.
Loop through the dictionary
When you need both the key and the value, items() is usually the easiest choice.
student_scores = {
"Alice": 88,
"Bob": 92,
"Cara": 78,
"Dan": 95
}
for name, score in student_scores.items():
print(name, score)
Output:
Alice 88
Bob 92
Cara 78
Dan 95
Here:
namegets each keyscoregets the matching valuestudent_scores.items()gives both parts together
For more help, see the Python dictionary items() method and how to loop through a dictionary in Python.
Find a result from the data
You can use the dictionary data to calculate a simple result.
In this case, you want the student with the highest score.
student_scores = {
"Alice": 88,
"Bob": 92,
"Cara": 78,
"Dan": 95
}
highest_name = max(student_scores, key=student_scores.get)
print("Top student:", highest_name)
print("Top score:", student_scores[highest_name])
Output:
Top student: Dan
Top score: 95
How this works:
max(student_scores, key=student_scores.get)checks the dictionary keys- for each key, it looks up the score with
student_scores.get - it returns the key with the largest value
So highest_name becomes "Dan".
Common beginner mistakes in this example
Here are some common problems beginners run into.
Using parentheses instead of square brackets
This is wrong:
print(student_scores("Alice"))
A dictionary is not a function, so this causes a TypeError.
Use square brackets instead:
print(student_scores["Alice"])
Misspelling a key name
This is wrong if the key does not exist:
print(student_scores["Alic"])
That causes a KeyError.
If you are not sure whether the key exists, use get() or check with in first.
if "Alic" in student_scores:
print(student_scores["Alic"])
else:
print("Key not found")
If you need help with this error, see KeyError in Python: causes and fixes.
Looping over keys only when both key and value are needed
This works, but it is less clear for beginners:
for name in student_scores:
print(name, student_scores[name])
A cleaner beginner-friendly version is:
for name, score in student_scores.items():
print(name, score)
Expecting dictionaries to keep data in sorted order automatically
A dictionary stores key-value pairs, but it does not automatically sort them by key or value.
If you need sorted output, you must sort it yourself.
When to use dictionary processing
Dictionary processing is useful when you want to connect readable names to values.
Common examples:
- counting things by name
- storing settings
- tracking scores or prices
- working with JSON-like data
- grouping related values under readable keys
If you want to see a related example, read the Python JSON to dictionary example.
FAQ
Is this page a dictionary tutorial?
No. It is a practical example page.
It shows one small real use case and points you to more detailed pages if you want to go deeper.
Should this page explain every dictionary method?
No. It only uses the methods needed for this example, such as items() and get().
For more detail, use the reference pages for those methods.
What is the best beginner way to loop through a dictionary?
Use:
for key, value in my_dict.items():
print(key, value)
This is usually the easiest way when you need both the key and the value.
How do I avoid KeyError in this example?
You can:
- use
get()when a key might be missing - check with
inbefore reading the value
For example:
name = "Eli"
if name in student_scores:
print(student_scores[name])
else:
print("Student not found")