|
| Home |
|---|
Iterating through dictionary values means accessing each value stored in a dictionary one by one.
While dictionaries store data in key-value pairs, sometimes we only need to work with the values. For example, calculating totals, displaying results, or analyzing stored data.
Python provides several simple ways to loop through dictionary values
using loops and the values() method.
The most common way to iterate through dictionary values is
by using the values() method.
This method returns all the values stored inside the dictionary. These values can then be accessed one by one using a loop.
for value in dictionary.values():
print(value)
The loop runs once for each value in the dictionary.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
for value in student.values():
print(value)
Alice
21
Python
The values() method returns all dictionary values.
The loop prints each value one at a time.
Notice that the keys are not printed, only the values.
Dictionary values are often used in calculations. For example, a dictionary might store marks, prices, or scores.
scores = {
"Math":85,
"Science":90,
"English":78
}
for value in scores.values():
print(value)
85
90
78
Each iteration prints the numeric values stored in the dictionary.
These values can later be used for calculations such as averages, totals, or comparisons.
We can also use dictionary values to perform mathematical operations.
scores = {
"Math":85,
"Science":90,
"English":78
}
total = 0
for value in scores.values():
total = total + value
print(total)
253
The program loops through each dictionary value
and adds it to the variable total.
The final result represents the sum of all values in the dictionary.
Sometimes we need dictionary values stored in a list for further processing.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
values_list = list(student.values())
print(values_list)
['Alice', 21, 'Python']
The list() function converts the dictionary values
into a Python list.
This allows us to apply list operations such as sorting or indexing.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net