Write a Python program to iterate over dictionaries using for loops


The program creates a dictionary d with four key-value pairs. A for loop is used to iterate over the items in the dictionary using the items method. The loop uses tuple unpacking to assign the key and value of each item to variables k and v respectively.

For each iteration of the loop, the key k and the value d[k] are printed using string concatenation and the print function. In summary, the program demonstrates how to iterate over the items in a dictionary in Python and how to access the values of a dictionary using the keys.

Source Code

d={"Name":"Ram" , "Age":23 , "City": "Salem", "Gender": "Male"}
for k, v in d.items():
     print(k, ' : ', d[k]) 

Output

Name  :  Ram
Age  :  23
City  :  Salem
Gender  :  Male