Python ProgrammingPython Programming

Python's program to iterating over dictionaries using 'for' loops

Example 1:

##
# Python's program to iterating over dictionaries using 'for' loops

x = {'z': 10, 'l': 50, 'c': 74, 'm': 12, 'f': 44, 'g': 19}

# Python 3
for key, val in x.items():
    print(key, "=>", val)

print("\n------------------------\n")

for key in x:
    print(key, "=>", x[key])

print("\n------------------------\n")
for key in x.keys():
    print(key, "=>", x[key])

print("\n------------------------\n")
for val in x.values():
    print(val)

Sample output of above program.
C:\programs\dictionary>python example.py
c => 74
f => 44
z => 10
l => 50
g => 19
m => 12

------------------------

c => 74
f => 44
z => 10
l => 50
g => 19
m => 12

------------------------

c => 74
f => 44
z => 10
l => 50
g => 19
m => 12

------------------------

74
44
10
50
19
12

C:\programs\dictionary>