Online Tutorials & Training Materials | STechies.com
Register Login

How to Iterate Through a Dictionary in Python with Example?

|| 0

How to Iterate Through a Dictionary in Python with Example?
Stechies

What is an 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 in Python

Dictionaries in Python are used to store key-value pairs in un-ordered manner . 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.

Iterating over the key-value pairs

In this example we iterate dictionary using “for” loop and getting each ‘key value pair’ by using items() function as output.

Example 1:

# Python3
# Iterating over dictionary with key-value pairs.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
"Mango" : "Green",
"Apple" : "Red",
"Grapefruit" : "Pink",
"Blackberry" : "Purple",
"Sapodilla" : "Brown"}

# For loop to Iterating over key & value
for fruit, color in fruitscolor.items():
print(fruit, ":", color)

Output:

Banana : Yellow
Mango : Green
Apple : Red
Grapefruit : Pink
Blackberry : Purple
Sapodilla : Brow

Example 2:

# Python3
# Iterating over dictionary with key-value pairs.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
"Mango" : "Green",
"Apple" : "Red",
"Grapefruit" : "Pink",
"Blackberry" : "Purple",
"Sapodilla" : "Brown"}

# For loop to Iterating over key & value
for fruit in fruitscolor:
print("{} = {}". format(fruit, fruitscolor[fruit]))

Output:

Banana = Yellow
Mango = Green
Apple = Red
Grapefruit = Pink
Blackberry = Purple
Sapodilla = Brown

In the above example, for loop is defined to iterate over keys of dictionary named ‘fruitscolor'. For each loop move it implicitly/ automatically pickup the value of loop key from the dictionary and so on.

Iterating over the keys in the dictionary.

In this example we iterate through dictionary using “for” loop and as output we are getting each key value.

Example:

# Python3
# Iterating over dictionary with key.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
"Mango" : "Green",
"Apple" : "Red",
"Grapefruit" : "Pink",
"Blackberry" : "Purple",
"Sapodilla" : "Brown"}

# For loop to Iterating over key
for key in fruitscolor:
print (key)

Output:

Banana
Mango
Apple
Grapefruit
Blackberry
Sapodilla

Iterating over the values in the dictionary

Here we are using “for loop” to put each item in the dictionary and getting the value of item by using value() function as output.

Example:

# Python3
# Iterating over dictionary with value.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
"Mango" : "Green",
"Apple" : "Red",
"Grapefruit" : "Pink",
"Blackberry" : "Purple",
"Sapodilla" : "Brown"}

# Using for loop Iterating over item and get value of item through value() function
for color in fruitscolor.values():
print(color)

Output:

Yellow
Green
Red
Pink
Purple
Brown

Preserve the order of keys and values in a dictionary

As we know that dictionary is the collection of un-ordered data so the order of key and value pair can be change every time when we run the code, to maintain the order of key value pair in the dictionary we use OrderedDict() function.

Example:

# Python3
# Iterating over dictionary with key-value pairs
# OrderedDict to print dictionary in specific order

from collections import OrderedDict

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
"Mango" : "Green",
"Apple" : "Red",
"Grapefruit" : "Pink",
"Blackberry" : "Purple",
"Sapodilla" : "Brown"}

# Get order of dictionary
fruitscolor = OrderedDict(fruitscolor)

# For loop to Iterating with key & value
for fruit, color in fruitscolor.items():
print(fruit, ":", color)

Output:

Banana : Yellow
Mango : Green
Apple : Red
Grapefruit : Pink
Blackberry : Purple
Sapodilla : Brown