Online Tutorials & Training Materials |
Register Login

Iterate through Dictionary Python with Examples

|| 0

Iterate through Dictionary Python with Examples

What is Iterator?

In Python, an iterator is an object that is used to traverse through all the elements in a collection or an array. This object has a countable amount of values. It implements the iterator protocol that has two methods namely, iter() and next().  

The iter() returns the iterator object and also defined the __getitem__ method. This method is also used for the initialization of the iterator object. The next() method is used to return the next element from the sequence of elements.

The iterator has next() method in Python 2 and __next__ method in Python 3.  When the iterator is used on a for loop, then the next() method is called on the object. The iterator raises a StopIteration to signal the end of an iteration.

Iterating Through Dictionary Python

Dictionaries in Python are used to store key-value pairs. There are basically three ways to iterate through a dictionary:

  • Iterating over the key-value pairs.
  • Iterating over the keys in the dictionary.
  • Iterating over the values in the dictionary.

The key-value pairs can be obtained using the keys() function that returns the value associated with each key.


dict = {'Ram': 1, 'day ': 2, 'morning':3} 
for t_key, t_value in dict.items():
    print( t_value, 'Belongs to',t_key )

Python Iterate with Implicit

Iteration is done with help of available key/ value in the dictionary definition at time declaration. Where the control moves automatically to available / defined keys /values there in view of dictionary for-loop.

For example, a dictionary is defined key and values are there first appear key and then values appeared in the dictionary. These iteration key values are assigned to the loop variable for iteration and the key is used to access the values from the dictionary.

dict = {"What is first name" : "Abraham", "what is your surname": "Khan"}
for key in dict:
	print("{} = {}". format(key, dict[key]))

Output of the program will be

  • what is your surname = Khan
  • What is first name = Abraham

Here if we made some changes in print statement then we will understand the difference.

dict = {"What is first name" : "Abraham", "what is your surname": "Khan"}
for key in dict:
	print("{} = {}". format(key, key)) 

Now Output will be

  • what is your surname = what is your surname
  • What is first name = What is first name

A for loop is defined in the above code that will iterate over keys of dictionary named 'dict ' . For each loop move it implicitly/ automatically pickup the value of loop key from the dictionary and so on.

Python Iterate with key

Key is a simple variable in python, but in the case of dictionary, it works for loop iteration.

weDct ={	'm': 'man be man',
          	'Maharathi' :'a person is Maharathi ',
          	'tugh': 'strong man '}
for key in weDct:
    print (key)

Output Will Be


Iterate Keys and Values in Python

Where a dictionary has value against a key. Multiple values can be saved for the similar data type for a single key.

dict = {'Ram': 1, 'day ': 2, 'morning':3} 
for t_key, t_value in dict.items():
    print( t_value, 'Belongs to',t_key )

Output will be

(1, 'Belongs to', 'Ram')
(2, 'Belongs to', 'day ')
(3, 'Belongs to', 'morning')

Iterate Values

Where a dictionary has a keys corresponding value. Iterating can also be 'values'.

webDict ={'man': 'will be man',
          'Marathi':'a person belongs to Maharashtra',
          'tough': 'remain strong against the force',
          'ruin': 'to spoil yourself '}
for value in webDict:
    print (webDict[value])

Output will be

a person belongs to Maharashtra
to spoil yourself
remain strong against the force
will be man