EN
Python - iterate through dictionary
0 points
In this article, we would like to show you how to iterate through dictionary in Python.
Quick solution:
xxxxxxxxxx
1
for key in dictionary:
2
print(key)
In this example, we use simple for loop to iterate through the dictionary and print all its keys.
xxxxxxxxxx
1
my_dict = {
2
"id": 1,
3
"name": "Tom",
4
"age": 25
5
}
6
7
for item in my_dict:
8
print(item)
Output:
xxxxxxxxxx
1
id
2
name
3
age
In this example, we use simple for loop to iterate through the dictionary and print all its values.
xxxxxxxxxx
1
my_dict = {
2
"id": 1,
3
"name": "Tom",
4
"age": 25
5
}
6
7
for key in my_dict:
8
print(my_dict[key])
Output:
xxxxxxxxxx
1
1
2
Tom
3
25