How to Iterate over dictionaries using ‘for’ loop in Python

In this text, we will see different ways to iterate over dictionaries with the help of a ‘for’ loop in Python. There are several ways of iterating over dictionaries with for loops in Python and in this text, we are going to discuss all of them with suitable examples. So, read this text to learn the concepts of iterating over dictionaries with for loops.

# create a dictionary depicting marks in different subjects
Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
# print the dictionary
print(Marks_subjects)
Output:
{'Maths': 98, 'English': 88, 'Hindi': 84, 'Science': 100, 'Social Science': 87}

This is a very simple way of printing the keys and values of a dictionary. The dictionary is printed as it is in the output section of our Python compiler. To make our output look more presentable and nice we use for loop.

For loop using keys and values:

This is one of the methods of iterating over dictionaries with for loop. In this method, we use keys and values in a for loop to iterate over a dictionary. The program is given below.

# create a dictionary depicting marks in different subjects
Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
# for loop using keys and values
for key,value in Marks_subjects.items():
    print(key,":",value)
Output:
Maths : 98
English : 88
Hindi : 84
Science : 100
Social Science : 87

Iterating using keys:

Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
print(Marks_subjects['English'])
Output:
 88

In the above code, we can see that we can print the value of a key using the above code. Hence, we can also use this same concept to iterate over dictionaries with a for loop using only the keys.

# create a dictionary depicting marks in different subjects
Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
# for loop using keys
for key in Marks_subjects:
    print(key,":",Marks_subjects[key])
Output:
Maths : 98
English : 88
Hindi : 84
Science : 100
Social Science : 87

We have used keys inside a for loop to iterarte over the dictionary.

Show all the keys and values of a dictionary using for loop:

This method is used to show all the keys and values of a dictionary separately with the help of a for loop. The program is given below.

For loop to show all the keys of the dictionary.

# create a dictionary depicting marks in different subjects
Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
# for loop to print all the keys
for key in Marks_subjects.keys():
    print(key)
Output:
Maths
English
Hindi
Science
Social Science

For loop to show all the values of the dictionary.

# create a dictionary depicting marks in different subjects
Marks_subjects={'Maths':98,'English':88,'Hindi':84,'Science':100,'Social Science':87}
# for loop to print all the values
for value in Marks_subjects.values():
    print(value)
Output:
98
88
84
100
87

Leave a Reply

Your email address will not be published. Required fields are marked *