How to Loop through Dictionary in Python?


Loop through Python Dictionary

Dictionary is a collection of items. Each item is a key:value pair. And all the items in a dictionary can be traversed using a looping statement or traversing technique.

To loop through a dictionary, we can use Python for loop. In this tutorial, we will learn how to iterate through key:value pairs of dictionary, or just the keys or just the values.


Examples

1. Iterate through items of given dictionary

In this example, we will initialize a dictionary with three key:value pairs. We shall use Python For Loop to iterate over this dictionary, and print the keys.

Python Program

myDictionary = {
	"name": "Lini",
	"year": 1989,
	"expertise": "data analytics"}

#iterate through dictionary
for key in myDictionary:
	print(key, '-', myDictionary[key])

Explanation

  1. A dictionary myDictionary is created with three key-value pairs: "name": "Lini", "year": 1989, and "expertise": "data analytics".
  2. A for loop is used to iterate over the dictionary, where key represents each key in the dictionary.
  3. Inside the loop, myDictionary[key] is used to access the corresponding value for the current key.
  4. The print() function is used to display each key followed by its value, separated by a hyphen. The output will be:
    • name - Lini
    • year - 1989
    • expertise - data analytics

Output

name - Lini
year - 1989
expertise - data analytics

All the keys are printed by traversing through the Python dictionary using for loop. And during each iteration, we could access the value corresponding to a key using indexing.


2. Loop through keys in given dictionary

To traverse exclusively through keys, you can use the default for item in iterable statement as shown below.

Python Program

myDictionary = {
	"name": "Lini",
	"year": 1989,
	"expertise": "data analytics"}

#iterate through dictionary
for key in myDictionary:
	print(key)

Explanation

  1. A dictionary myDictionary is created with three key-value pairs: "name": "Lini", "year": 1989, and expertise: "data analytics".
  2. A for loop is used to iterate over the dictionary, where key represents each key in the dictionary.
  3. The print() function is used to display each key. The output will be:
    • name
    • year
    • expertise

Output

name
year
expertise

3. Loop through values in given dictionary

To traverse exclusively through values, you can use the default for item in iterable statement as shown below.

Python Program

myDictionary = {
	"name": "Lini",
	"year": 1989,
	"expertise": "data analytics"}

#iterate through dictionary values
for value in myDictionary.values():
	print(value)

Explanation

  1. A dictionary myDictionary is created with three key-value pairs: "name": "Lini", "year": 1989, and "expertise": "data analytics".
  2. The values() method is called on myDictionary, which returns a view object containing all the values of the dictionary.
  3. A for loop is used to iterate over the values in the dictionary, where value represents the current value in each iteration.
  4. The print() function is used to display each value. The output will be:
    • Lini
    • 1989
    • data analytics

Output

Lini
1989
data analytics

In the above example, we have used dict.values(). dict.values() returns iterator over only the values in the dictionary.


4. Loop through key:value pairs in given dictionary

Or you can use for loop, to access both key and value, as shown below.

Python Program

myDictionary = {
	"name": "Lini",
	"year": 1989,
	"expertise": "data analytics"}

#iterate through key:value pairs of dictionary
for key, value in myDictionary.items():
	print(key, ': ', value)

myDictionary.items() returns an iterator that provides access to both key and value.

Explanation

  1. A dictionary myDictionary is created with three key-value pairs: "name": "Lini", "year": 1989, and "expertise": "data analytics".
  2. The items() method is called on myDictionary, which returns a view object containing the key-value pairs as tuples.
  3. A for loop is used to iterate through each key-value pair, where key and value represent the current key and corresponding value in each iteration.
  4. The print() function is used to display each key followed by its value, separated by a colon. The output will be:
    • name : Lini
    • year : 1989
    • expertise : data analytics

Output

name :  Lini
year :  1989
expertise :  data analytics

Summary

In this tutorial of Python Examples, we learned how to traverse through the Dictionary items using for loop with the help of well detailed examples.


Python Libraries