Python dictionary stores the data in a key-values pair format, it allows the developers to store large amount/variety of data; therefore, there were several ways to iterate over them. In this tutorials we are going to see how to Iterate over dictionary Python in different ways.

How to Iterate over dictionary Python:

As a dictionary stores the data as key-value pair, it allows us to loop through the keys or values.

The following dictionary consists of persons information such as user_name,first_name and last_name. Lets try to iterate this.

persons = {
    'user_name':'chandra',
    'first_name':'chandra shekhar',
    'last_name':'goka'
}

1.Loop through key-value pairs:

We could loop through dictionary using a for loop like below.

persons = {
    'user_name':'chandra',
    'first_name':'chandra shekhar',
    'last_name':'goka'
}

#Looping thorough key,values
for key,value in persons.items():
    print(f"Key: {key} - Value: {value}")

On the above for loop we created names for the two variables key and value (you can choose any names for these). items() method returns the key-value pair for a specific iteration and the the loop assigns key-values into the key,value variables for the each iteration.

Output:

Key: user_name - Value: chandra
Key: first_name - Value: chandra shekhar
Key: last_name - Value: goka

2.Loop through Keys in a Dictionary:

The key() method is used to get the key from a dictionary, using which we can loop though the keys. The below dictionary is a collection of favorite subject of each person. Lets loop though this Dictionary using keys.

favorite_subjects={
    'chandra':'Computers',
    'panvith':'Physics',
    'jon':'Mathematics',
    'robert':'Computers'
}

for name,fav_subject in favorite_subjects.items():
    print(f"{name} likes {fav_subject}")

Output:

chandra
panvith
jon
robert

3.Loop through keys in order:

From Python 3.7 looping through a dictionary gives the items the same way the items were inserted. If you would like to read the items in an sorted order, we can do this by applying sorted() method on it.

favorite_subjects={
    'chandra':'Computers',
    'panvith':'Physics',
    'jon':'Mathematics',
    'robert':'Computers'
}

#Keys in Order
for name in sorted(favorite_subjects.keys()):
    print(name)

Output:

chandra
jon
panvith
robert

4.Loop through Values in a Dictionary:

Just like loop though the keys, we can also loop through the values of a dictionary using values() method.

favorite_subjects={
    'chandra':'Computers',
    'panvith':'Physics',
    'jon':'Mathematics',
    'robert':'Computers'
}
for fav_subject in favorite_subjects.values():
    print(fav_subject)

Output:

Computers
Physics
Mathematics
Computers

5.Loop through keys in order:

favorite_subjects= {
    'chandra':'Computers',
    'panvith':'Physics',
    'jon':'Mathematics',
    'robert':'Computers'
}

#Values in Order
for fav_subject in sorted(favorite_subjects.values()):
    print(fav_subject)

Output:

Computers
Computers
Mathematics
Physics

As you can see on the above output, there was a duplicate subject (Computer), we can remove the duplicates while iterating the dictionary values like below.

6.Loop through Values and remove duplicates:

Using set() method we can remove the duplicates – You can see more about set function here.

favorite_subjects= {
    'chandra':'Computers',
    'panvith':'Physics',
    'jon':'Mathematics',
    'robert':'Computers'
}

#Remove duplicates in values
for fav_subject in sorted(set(favorite_subjects.values())):
    print(fav_subject)

Output:

Computers
Mathematics
Physics

References:

Happy Learning 🙂