Languages
[Edit]
EN

Python - iterate through dictionary

0 points
Created by:
Admin Dirask Community
1810

In this article, we would like to show you how to iterate through dictionary in Python.

Quick solution:

for key in dictionary:
    print(key)

 

Practical example

Example 1 - print keys

In this example, we use simple for loop to iterate through the dictionary and print all its keys.

my_dict = {
    "id": 1,
    "name": "Tom",
    "age": 25
}

for item in my_dict:
    print(item)

Output:

id
name
age

Example 2 - print values

In this example, we use simple for loop to iterate through the dictionary and print all its values.

my_dict = {
    "id": 1,
    "name": "Tom",
    "age": 25
}

for key in my_dict:
    print(my_dict[key])

Output:

1
Tom
25

Python - dictionary (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join