Python Program - Iterate Over Dictionaries Using for Loop

To iterate over dictionaries using a for loop in Python, you can use the items() method. Here's an example program:

fruits = {"apple": 1, "banana": 2, "cherry": 3}

for fruit, quantity in fruits.items():
    print("Fruit:", fruit, "Quantity:", quantity)
Sou‮ecr‬:www.theitroad.com

In this program, the items() method is used to loop over the fruits dictionary and retrieve both the key and value of each item. The fruit variable will contain the current key being processed, and the quantity variable will contain the value of the corresponding item. The print() function is then used to display the key and value of each item.

When you run this program, the output will be:

Fruit: apple Quantity: 1
Fruit: banana Quantity: 2
Fruit: cherry Quantity: 3