Last Updated on June 17, 2021 by Roshan Parihar
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 Python and use only keys or values in your programming. In the dictionary variable, the keys are related to their 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 the keys of the 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 prints the keys in the output by using the print statement.
1 2 3 4 5 6 7 8 9 10 |
myDict = { "one": "Ram", "two": "Shyam", "three": 10, "fore": "Bilal", "five": 13.2, "six": "Feroz" }; for key, value in myDict.items(): print(key) |
Output
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 key. 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.
1 2 3 4 5 6 7 8 9 10 |
myDict = { "one": "Ram", "two": "Shyam", "three": 10, "fore": "Bilal", "five": 13.2, "six": "Feroz" }; for key, value in myDict.items(): print(value) |
Output
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 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 their value in the output.
1 2 3 4 5 6 7 8 9 10 |
myDict = { "one": "Ram", "two": "Shyam", "three": 10, "fore": "Bilal", "five": 13.2, "six": "Feroz" }; for key, value in myDict.items(): print(key, 'Relates to', value) |
Output
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.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read
- Resolve the error showing TypeError: ‘list’ object is not callable’ in Python
- How to loop over Python list variable
- Check data type in Python with easy examples
- How to Declare or create variables in python
I hope you like this tutorial on how to loop through dictionary elements in Python.
References