How to Loop through Dictionary Values in Python?
Python Dictionary - Loop through Values
A dictionary is a collection of key:value pairs. At times, it is required to loop through only the values in a Dictionary.
You can access the values alone of a Python Dictionary using dict.values() on dictionary variable. Then use Python For Loop to iterate through those values.
Examples
1. Loop through values in given dictionary using values()
In this example, we will initialize a dictionary with some key:value pairs, and use for loop to iterate through values in the dictionary.
Python Program
myDictionary = {
"name": "Lini",
"year": 1989,
"expertise": "data analytics"}
#iterate through dictionary
for value in myDictionary.values():
print(value)
Output
Lini
1989
data analytics
2. Loop through values in given dictionary using items()
Similar to Dictionary.values(), the function Dictionary.items() returns key,value during each iteration. You can access values from the key:value, while traversing through the dictionary.
Python Program
myDictionary = {
"name": "Lini",
"year": 1989,
"expertise": "data analytics"}
#iterate through dictionary
for key,value in myDictionary.items():
print(value)
Output
Lini
1989
data analytics
Summary
In this tutorial of Python Examples, we learned how to iterate through values of a Dictionary with the help of well detailed examples.