How to Loop Through Dictionary Elements in Python

In this tutorial, learn how to loop through dictionary elements in Python. To loop or iterate over each element of a dictionary variable, you have to use the for loop of Python.

You can get the dictionary variable keys and values in the output. Use the for loop of the Python and use only keys or values in your programming. In the dictionary variable, the keys are related to its relevant values. If you want to get the values, you have to call its matching key.

Loop Through Dictionary Elements and Print Keys

If you want to use only the keys of the dictionary variable in our programming. You have to use the below code to get keys of dictionary variable in the output.

It uses the for loop to iterate or loop through dictionary elements in Python. After each iteration of the loop, it print the keys in the output by using the print statement.

Output

one
two
three
fore
five
six

Check the above output of the for loop. It contains only the keys after each iteration of the loop. However, you can print and use the values also using the below-given example. Read further to find out how to get only the values of the dictionary in python.

How to Iterate Over Dictionary Items and Print Values

If you are working on Python and want to use only the values. You have to use the below example showing iteration through each element of the dictionary variable.

After the iteration or loop, it prints out the values given with each keys. In the above example, you get the keys in the output. However, in this example, you will get only the values of the dictionary variable.

Output

Ram
Shyam
10
Bilal
13.2
Feroz

The above example contains only the values in the output. It prints out all the values in the output using the print statement. However, you can use both keys and values in the output using the below-given example. To get both keys and values, you have to read further.

Print Both Keys and Values of Python Dictionaries

In addition to the above all example, if you want to get both keys and the values in the output. You can use the below given which gives you both keys and values in the output.

You just have to add the keys and values as the argument of the print statement in comma separation. After each iteration of the for loop, you will get both the keys its relevant values in the output.

You can also add the text between the keys and its value in the output.

Output

one Relates to Ram
two Relates to Shyam
three Relates to 10
fore Relates to Bilal
five Relates to 13.2
six Relates to Feroz

The above example contains both the keys and the values in the output. The text ‘Related to’ in the output showing the given key is related to the given value in the output.

You may also like to read

Hope, you like this tutorial of how to loop through dictionary elements in Python. If you have any query regarding the tutorial, please comment below.

References

Also tell me, which method you are using to iterate through each element of dictionary variable in Python.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.