Iterate Over Dictionaries in Python & Print Items in Key-value in Pair

In this blog, we’ll learn how to Iterate Over Dictionaries in Python and Print Items in Key-value in Pair. Python dictionaries are a useful data structure for storing data in key-value pairs. To execute certain tasks, you may need to loop through all of the key-value pairs in a dictionary. The items() method in Python makes it simple to iterate over dictionaries. This function provides a view object containing the dictionary’s key-value pairs as tuples, which can subsequently be unpacked into variables for further processing.

Generally, we use a for loop to iterate over dictionaries but here we’re showing an example of a Python program to iterate over dictionaries and print items in key-value in pairs. We will also discuss the syntax and operation of the code that enables us to print the data in a clear and organised manner. So, let’s dive in and explore how to use for loops to iterate over dictionaries in Python!

Python Program to Iterate Over Dictionaries and Print Items in Key-value in Pair

# Iterate Over Dictionaries in Python and Print Items in Key-value in Pairs
# Print items in Key-Value in pair

countryAndCode = {
	'India': 'IND',
	'Afghanistan': 'AFG',
	'Canada': 'CAN',
	'Russia': 'RUS'
}

keys = countryAndCode.items()
# we are printing the key values data in the form of pairs
# all the pairs are enclosed in a dictionary.
print(keys)

Code Logic and Explanation:

  • Creating a dictionary containing key-value pairs
    • First, we create a dictionary named ‘countryAndCode’ with key-value pairs that represent country names and their corresponding codes respectively.
    • This dictionary is created using curly braces {} with each key-value pair separated by a comma.
  • Storing dictionary items in a variable
    • Next, we store the dictionary items in the variable named ‘keys’ using the items() method. 
    • This step returns a view object containing the key-value pairs of the dictionary as tuples.
    • The ‘keys’ variable now contains the following tuples: (‘India’, ‘IND’), (‘Afghanistan’, ‘AFG’), (‘Canada’, ‘CAN’), (‘Russia’, ‘RUS’).
  • Printing items in Key-Value pairs
    • The key-value pairs from the ‘keys’ variable are printed using the print statement.
    • The pairs are enclosed in a dictionary with each pair separated by a comma.
    • The print statement prints the entire dictionary.

In this tutorial, we create a dictionary named countryAndCode which contains country names as keys and their respective country codes as values. The .items() function extracts all the keys and values from the dictionary and is stored in the keys variable.

The code output is a list of tuples containing the keys and values of the dictionary in the order they were added to the dictionary. Each tuple is enclosed in brackets, and commas will separate the key-value pairs. This list of tuples represents the key-value pairs of the original dictionary, printed in pairs.

Output:

dict_items([('India', 'IND'), ('Afghanistan', 'AFG'), ('Canada', 'CAN'), ('Russia', 'RUS')])

Some Alternate methods to do the Iterate Over Dictionaries

Some of the other alternatives for iterating over dictionaries in Python is using the keys(), values(), or items() method along with a for loop. The advantage of using the method explained above is that it offers a simple and easy understanding, especially for beginners. Additionally, it allows you to iterate over both the keys and values of the dictionary simultaneously, which can be useful in many scenarios. 

In this blog, we learnt how iterating over dictionaries in Python is a straightforward process. We can easily access the key-value pairs using the items() method and then print them in pairs using a loop. This makes it easy to work with the data stored in dictionaries and perform operations on it. It is essential to understand the basics of dictionary iteration as it is a fundamental concept in Python programming used frequently in various applications. With practice, one can become proficient in using dictionaries and other data structures in Python, making it easier to handle complex data and solve problems effectively.

FAQ – Do the Iterate Over Dictionaries

Can I add a dictionary to another dictionary in Python?

Yes, you can add a dictionary to another dictionary in Python by using the update() method.

What is the difference between a view object and a list in Python?

A view object is a dynamic object that provides a view of a dictionary’s contents. At the same time, a list is a static object that contains an ordered sequence of elements.

What happens when I try to access a key that doesn’t exist in a dictionary?

When you try to access a key that doesn’t exist in a dictionary, a KeyError exception is raised.

How can I delete a key-value pair from a dictionary in Python?

You can delete a key-value pair from a dictionary in Python by using the del statement.

About The Author

Leave a Reply