Iterate Over Dictionaries in Python Using Items() Function

(Last Updated On: 26/09/2023)

In this tutorial, we will learn how to iterate over dictionaries in Python using items() function. Python dictionaries are a powerful data structure that allows you to store data in key-value pairs. Often, you may need to loop through all the key-value pairs in a dictionary to perform certain operations. You can also easily iterate over dictionaries using the key() function. When working with dictionaries, it is often necessary to iterate over them to perform operations such as printing, updating, or filtering the values. 

We will explore how to use the items() function to iterate over dictionaries in Python. We will provide a detailed explanation of the items() function and its syntax, as well as a Python program to demonstrate how to use it to iterate over a dictionary. With this knowledge, you will be able to efficiently iterate over and manipulate dictionaries in your Python code. So, let’s dive in and learn how to Iterate Over Dictionaries in Python Using the Items() Function!

What is the Items() Function?

In Python, the items() function is a built-in method that can be used to access a dictionary’s key-value pairs in the form of a list of tuples. It returns an iterable object containing all the key-value pairs present in the dictionary. This function is commonly applied when we loop through all the items of a dictionary in a for loop or when we want to access specific key-value pairs in a dictionary. In short, the items() function provides an easy and efficient way to iterate over dictionaries in Python.

Python Program to Iterate Over Dictionaries Using Items() Function

# Iterate Over Dictionaries in Python Using Items() Function
# Iterate through all key, and value pairs using items()

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

print('List of given Country and their Code:\n')

# Iterating over values
#  printing all the key and value pairs present in a dictionary using a items() method.
for country, code in countryAndCode.items():
	print(country, ":", code)

Code Logic and Explanation

  • Creating a dictionary containing key-value pairs
    • First, we create a dictionary containing key-value pairs. 
    • In the given code, we have created a dictionary named ‘countryAndCode’ which consists of 4 key-value pairs.
  • Printing the list of countries and their respective codes
    • We will use the ‘items()’ function to print the list of countries and their respective codes.  
    • A ‘for’ loop is initialized to iterate over the key-value pairs in the dictionary. 
    • In this code we use ‘for country, code in countryAndCode.items():’. 
    • By this, we are iterating over the dictionary and assigning the country to the variable ‘country’ and the code to the variable ‘code’ in each cycle. 
  • Printing the key-value pairs of the dictionary
    • We use the ‘print()’ function to print each country and its corresponding code in the format ‘country : code’.
  • Displaying the Output
    • Finally, the output is displayed using the ‘print()’ function. The list of countries and their codes are printed on the console using the ‘print()’ function.

Output:

List of given Country and their Code:

India : IND
Afghanistan : AFG
Canada : CAN
Russia : RUS

This code makes a dictionary called countryAndCode with four key-value pairs, where the keys are country names and the values are their respective country codes. Then, using a for loop and the items() function, we iterate through each key-value pair in the dictionary and print them out in a formatted way. 

Specifically, for each iteration of the loop, we assign the current key to the variable country and the current value to the variable code, and then print them out with a colon separator. The output of the code is a list of the given countries with their respective country codes printed below the text “List of given Country and their Code:”.

Comparison – Using the Items() Function to Iterate Over Dictionaries

Although there are other methods available to iterate over dictionaries, such as the keys() and values() methods, the items() method used above is a more efficient way of repeating over a dictionary. 

The keys() method returns a list of all the keys in the dictionary, while the values() method returns a list of all the values in the dictionary. These methods are helpful if you only need to iterate over the keys or values of a dictionary. On the other hand, the items() method returns a list of tuples, where each tuple contains the key and value of a dictionary item. This method is useful when you need to iterate over both the keys and values of a dictionary.

In this program, the items() method allows us to iterate through all the key-value pairs of a dictionary. It is a very useful method for accessing and manipulating the elements in a dictionary and we can easily retrieve the keys to perform various operations on them. In addition, iterating over a dictionary using items() can also help us save time and avoid repetitive code. Overall, the items() method is a valuable tool to have in our Python programming toolkit, and it is important for any developer working with dictionaries.

Some FAQs on Iterate Over Dictionaries using items()

When using the items() function, can the order of the key-value pairs be guaranteed?

When using the items() function, the order of key-value pairs in a dictionary cannot be guaranteed.

What happens when I use the items() function on a non-dictionary object?

If the items() function is used on a non-dictionary object, a TypeError will be raised.

What is the difference between the keys() and values() functions in Python?

The keys() function returns a view object that contains the keys of a dictionary, while the values() function returns a view object that contains the values of a dictionary.

Can I modify the key-value pairs of a dictionary while iterating over them?

No, the key-value pairs of a dictionary cannot be modified while iterating over them using the items() function.

About The Author

Leave a Reply