Iterate Over Dictionaries in Python Using for Loop

(Last Updated On: 26/09/2023)

In this tutorial, we’ll learn how to iterate over dictionaries in Python using for loop. Dictionaries are an essential data structure in Python that allows you to store key-value pairs. 

One of the most common operations is a Python program to iterate over dictionaries using for loop to access the keys and values. You can also use a for loop to iterate over dictionaries and access either their keys, values, or both. We will discuss how to iterate over dictionaries using a for loop and access their keys without using the key() method. The for loop is a versatile loop that allows you to iterate over any sequence, including dictionaries.

Specifically, we will show you how to access the keys of a dictionary without using a key() function. Using a practical example of a dictionary of countries and their corresponding country codes, we will demonstrate how to iterate over a dictionary in Python using for loop. By the end of this blog, you will have a better understanding of how to use a for loop to iterate over dictionaries in Python.

Python Program to Iterate Over Dictionaries Using for Loop

# Iterate Over Dictionaries in Python Using for Loop
# Access key without using a key()

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

print('List of given Country:\n')

# Iterating over dictionaries using 'for' loops for iterating our keys
for Country in countryAndCode:
	# printing all the keys present in the Dictionary.
	print(Country)

Code Logic and Explanation

  • Defining a dictionary
    • In this step, we define a dictionary named countryAndCode with the keys and their respective values.
    • Each key-value pair in the dictionary represents a country and its respective code.
  • Print the list of countries
    • We use a print statement to display the list of countries present in the dictionary. 
    • It uses the ‘\n’ escape sequence to print the countries in a separate line.
  • Iterate over the dictionary using a for loop
    • Now a for-loop is used to iterate over the countryAndCode dictionary. 
    • This loop iterates over the keys of the dictionary.
  • Accessing the keys of the dictionary
    • We now use the Country variable to access the keys of the dictionary in each iteration of the for loop.
    • This will be used to print the countries present in the dictionary.
  • Displaying the output
    • To display the list of countries present in the countryAndCode dictionary, we use the print() statement. 
    • The output displays the list of countries in the dictionary, with each country printed on a new line.

This code explains how we can create a dictionary named ‘countryAndCode’, with keys representing the names of countries and values representing their corresponding codes. The code then uses a ‘for’ loop to iterate through the dictionary and prints out the names of the countries. In each iteration, the loop assigns the current key (i.e., the name of the country) to the variable ‘Country’, which is then printed using the ‘print()’ function.

Output:

The output of the code will be a list of country names as follows:

List of given Country:

India
Afghanistan
Canada
Russia

We Use for Loop to Iterate Over Dictionaries

In addition to the above method, you can use the items() and keys() methods to iterate over dictionaries in Python. The items() method allows you to iterate over both keys and values of the dictionary at the same time. While with the keys() method you can return a list of all the keys in the dictionary, which can be iterated over using a for loop.

We prefer iterating over dictionaries in Python using for loop as it is the simplest and most efficient way to iterate over the keys of a dictionary. Since it does not require creating any additional lists or objects, it helps reduce the complexities in our code.

In this tutorial, we learnt how iterating over dictionaries in Python using a for loop can be a very useful tool for accessing and manipulating data within a dictionary. We also learned how to access values and key-value pairs using the values() and items() methods.

Iterating over dictionaries is a common task in Python programming, and using a for loop simplifies the process. It is an essential skill for data scientists and developers working with Python.

Some FAQs on Iterate Over Dictionaries in Python Using for Loop

How are the keys in the dictionary accessed in the for loop?

The keys in the dictionary are accessed using the variable ‘Country’ in the for loop.

How is the output formatted in this program?

The output is formatted using the ‘print’ statement and a newline character (‘\n’) to separate each key.

Can I access the values in the dictionary using this program?

No, this program only accesses the keys in the dictionary. To access the values, you need to use a separate statement.

What is the advantage of using a for loop to iterate over a dictionary in Python?

Using a for loop to iterate over a dictionary in Python is advantageous because it allows for easy access to the keys and values in the dictionary, and provides a concise and efficient way to loop through the data.

About The Author

Leave a Reply