Iterate Over Dictionaries in Python Using Values() Function

In this tutorial, we’ll learn how to iterate over dictionaries in Python using values() function. Dictionaries are an essential data structure in Python that allow us to store and manipulate data in a key-value format. They are widely used in various programming applications, from web development to scientific computing. 

One of the most useful techniques is iterating over a dictionary using the values() function to extract values or keys. Python provides multiple methods to iterate over a dictionary, including the keys() function.

In this blog, we will use this function to extract all the values from a dictionary and perform operations on them, without having to worry about the associated keys. Also, we will discuss how to Iterate Over Dictionaries in Python Using the Values() Function and demonstrate how to write a Python Program to Iterate Over Dictionaries Using Values() Function.

What are Values() Function?

In Python, values() is a built-in function that can be used to iterate over the values of a dictionary. We can use it to return a view object that contains the values of a dictionary. This function is often used in for loops to loop through the values of a dictionary without having to reference the keys. By using values(), you can access all the values present in a dictionary and perform various operations on them.

Python Program to Iterate Over Dictionaries Using Values() Function

# Iterate Over Dictionaries in Python Using Values() Function
# Iterate through all values using .values()

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

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

# Iterating over values
# we are using the values() method
for code in countryAndCode.values():
	#  print all the values present in the dictionary.
	print(code)

Code Logic and Explanation

  • Creating a dictionary

We create a dictionary named countryAndCode to store the mapping of countries and their respective codes. Here, the keys represent the country names and values represent the country codes.

  • Printing the initial message

Using the print function, we display the message “List Of given Country:\n”. This message serves as an introduction to the list of countries and their codes.

  • Iterating the values of the dictionary

Our program’s main logic of the code lies in this section, where we use a for loop to iterate over the values of the countryAndCode dictionary. This is done using the .values() method, which returns a list of all the values in the dictionary.

  • Displaying the values of the dictionary

Inside the for loop, we use the print function to display each value of the dictionary using the values() method. In this case, the values represent the country codes.

Output:

List Of given Country:

IND
AFG
CAN
RUS

In this tutorial, the code creates a dictionary named countryAndCode with four key-value pairs. Here the keys are the names of countries and the values are their respective country codes. In the next step, we use the for loop to iterate over the values of the countryAndCode dictionary using the values() function. This only iterates the loop over the values of the dictionary and not the keys.

Inside the loop, the print statement is used to print each value (i.e., the country code) present in the dictionary. Therefore, the output of this code will display a list of country codes, each on a new line, which corresponds to the values of the countryAndCode dictionary.

Why We Use Values() Function to Iterate Over Dictionaries

You can use the items() method or even apply the for loop to directly iterate over dictionaries. Using the items() method you can return a list of tuple pairs, where each pair contains a key-value pair of the dictionary. And by using the keys() method you can return a list of keys present in the dictionary.

The values() method is preferred when you only want to access the values of the dictionary and not the keys. It is more efficient than using the items() method since it does not have to create new tuples containing both keys and values. Additionally, using values() provides a cleaner and more efficient program since you don’t have to write extra code to extract only the values.

We can see that iterating over dictionaries in Python with the values() function is a powerful tool that allows us to retrieve all the values from a dictionary without worrying about the keys. This function makes it easier to iterate through a dictionary and execute specific operations on its values. We can quickly access and operate with each value in a dictionary by using a for loop and the values() method and assists us in writing more efficient and effective code.

FAQs – Iterate Over Dictionaries in Python Using Values()

Can I access and modify the values of the keys of a dictionary using the values() function?

No, the values() function only returns the values of a dictionary, not the keys and does not allow for modification of the values.

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

The keys() function returns a list of all the keys in a dictionary, while the values() function returns a list of all the values in a dictionary.

What will happen when I access a key that does not exist in a dictionary using the values() function?

The values() function does not check for the existence of keys, so it will simply return a list of values for all the keys in the dictionary.

How can I check if a particular value exists in a dictionary using Python?

You can use the ‘in’ keyword to check if a particular value exists in a dictionary.

About The Author

Leave a Reply