Loop a dictionary in Python

Author: Al-mamun Sarkar Date: 2021-04-11 02:31:46

We can iterate a dictionary using the dictionary, item(), keys(), values() by a for loop.

Looping a Dictionary:

info = {
    'name': 'Jone', 
    'last_name': 'Doe', 
    'email': 'jon@example.com', 
    'phone': '124242344'
}

for x in info:
    print(x)

Output:

name
last_name
email
phone

Getting the value:

for x in info:
    print(info[x])

Output:

Jone
Doe
jon@example.com
124242344

 

Use loop on values():

for item in info.values():
    print(item)

 Use loop on keys():

for item in info.keys():
    print(item)

  Getting keys and values using items():

for key, val in info.items():
    print(key, val)