Python Dictionary – Loop through Values

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.

Example 1: Loop through Dictionary Values using Dictionary.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)
Run

Output

Lini
1989
data analytics

Example 2: Dictionary Values from Dictionary.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)
Run

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.