Loop a dictionary in Python

Loop a dictionary in Python

Instructor-svg Al-Mamun Sarkar
Apr 11 , 2021

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)

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram