Languages
[Edit]
EN

Python - iterate through dictionary

0 points
Created by:
Admin Dirask Community
4380

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

Alternative titles

  1. Python - loop through the dictionary
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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