How to traverse a dictionary in python || with 2 easy examples

How to traverse a dictionary in python

How to traverse a dictionary in python

Traversing means Iterating a Python dictionary. we can traverse a dictionary in python with for loop.

Example 1 :-

d = { 1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 : ‘Four’ }
for i in d:
    print(d[i])

Output

One
Two
Three
Four

Example 2 :-

d = { 1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 : ‘Four’ }
for i in d:
    print(i,”:”,d[i])

Output

1 : One

2 : Two

3 : Three

4 : Four

Important Links: Informatics Practices

What is Python Dictionary
How to create Python Dictionary
Dictionary functions in Python with examples
What is Python List
Characteristics of Python List
How to create Python List
How to Traverse a List in Python
How to Manipulate List in Python
How to Modify multiple elements in Python List
Slicing in Python List
How to delete elements from List in Python

If you like my post please share it with your friends by simply clicking the below social media button, and Don’t forget to comment so we will update ourselves.

[sm-youtube-subscribe]

Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *