Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Loop Dictionaries (Complete Guide for Beginners)

 A dictionary in Python stores data in key-value pairs, and looping through a dictionary is one of the most important skills when working with real-world data.

In this tutorial, you will learn different ways to loop through dictionaries using Python with clear examples.


What is Looping in a Dictionary?

Looping means iterating through dictionary elements one by one.

You can loop through:

  • Keys only
  • Values only
  • Both keys and values

Example Dictionary

student = {
    "name": "John",
    "age": 20,
    "grade": "A"
}

Loop Through Dictionary Keys (Default Method)

When you loop through a dictionary directly, Python returns keys only.

Example

for key in student:
    print(key)

Output

name
age
grade

Explanation

  • The loop automatically iterates through dictionary keys.

Loop Through Keys Using keys() Method

You can also use the keys() method for clarity.

Example

for key in student.keys():
    print(key)

Output

name
age
grade

Loop Through Dictionary Values

To get only values, use the values() method.

Example

for value in student.values():
    print(value)

Output

John
20
A

Explanation

  • Only dictionary values are printed.
  • Keys are ignored.

Loop Through Key-Value Pairs

To access both keys and values, use the items() method.

Example

for key, value in student.items():
    print(key, ":", value)

Output

name : John
age : 20
grade : A

Explanation

  • items() returns each key-value pair as a tuple.
  • You can unpack them directly in the loop.

Loop Through Dictionary in Sorted Order

You can loop through sorted keys using sorted().

Example

for key in sorted(student.keys()):
    print(key, student[key])

Output

age 20
grade A
name John

Loop Through Dictionary in Reverse Order

Example

for key in reversed(list(student.keys())):
    print(key, student[key])

Output

grade A
age 20
name John

Nested Dictionary Looping

Dictionaries can contain other dictionaries.

Example

students = {
    "student1": {"name": "John", "age": 20},
    "student2": {"name": "Alice", "age": 22}
}

Loop Example

for student_id, info in students.items():
    print(student_id)
    for key, value in info.items():
        print(" ", key, ":", value)

Output

student1
  name : John
  age : 20
student2
  name : Alice
  age : 22

Loop Through Dictionary with Condition

You can filter values while looping.

Example: Print only values greater than 20

data = {
    "a": 10,
    "b": 25,
    "c": 30
}

for key, value in data.items():
    if value > 20:
        print(key, value)

Output

b 25
c 30

Real-World Example: Product Inventory

inventory = {
    "Laptop": 10,
    "Mouse": 25,
    "Keyboard": 15
}

for product, quantity in inventory.items():
    print(product, "=>", quantity)

Output

Laptop => 10
Mouse => 25
Keyboard => 15

Real-World Example: User Profiles

users = {
    "user1": {"name": "John", "role": "admin"},
    "user2": {"name": "Sarah", "role": "editor"}
}

for user_id, info in users.items():
    print(user_id, ":", info["name"], "-", info["role"])

Output

user1 : John - admin
user2 : Sarah - editor

Common Mistakes

Mistake 1: Looping Only Keys but Expecting Values

❌ Wrong

for item in student:
    print(item)  # only keys

Mistake 2: Forgetting items() for Key-Value Pairs

❌ Wrong

for key, value in student:
    print(key, value)

This causes an error.


Correct Way

for key, value in student.items():
    print(key, value)

Dictionary Loop Methods Summary

MethodDescription
for key in dictLoop through keys
keys()Explicit key looping
values()Loop through values
items()Loop through key-value pairs
sorted()Loop in sorted order
reversed()Loop in reverse order

Practice Exercise 1

Print only keys from the dictionary.

car = {
    "brand": "Toyota",
    "model": "Corolla",
    "year": 2025
}

Practice Exercise 2

Print only values from the dictionary.

employee = {
    "name": "David",
    "salary": 5000,
    "city": "London"
}

Conclusion

Looping dictionaries in Python is essential for handling real-world data structures. You can:

  • Loop through keys
  • Loop through values
  • Loop through key-value pairs
  • Use sorting and filtering
  • Handle nested dictionaries

Mastering dictionary loops will make you more confident in Python programming, especially in data processing, APIs, and backend development.




Post a Comment

0 Comments