This Python article will provide an in-depth understanding of the Python dictionary values() method, which allows us to access all the values stored in a dictionary.
In addition, we also discuss its usage and some practical examples.
Dictionary values() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python Dictionary values() method
- Syntax of the values() method
- Purpose and use cases of the values() method
Python Dictionary values() method
The values() method is a built-in Python function that returns a view object containing the values of the dictionary. This view object is dynamic and will reflect any changes made to the dictionary.
Syntax:
The syntax for the values() method is as follows:
dict.values()
Here, ‘dict’ is the dictionary for which we want to retrieve the values.
values() method in Python Dictionary Examples
Let’s dive into some examples to better understand the values() method in action:
Example#1 Using the values() Method
us_president = {
'name': 'Joe Biden',
'age': 79,
'party': 'Democratic'
}
us_president_values = us_president.values()
print(us_president_values)
In this example, we have a Python dictionary called 'us_president'
that contains information about the current US president, Joe Biden. By calling the values() method, we obtain a view object containing all the values in the Python dictionary.
Output:
Example#2 Analyzing Demographic Data
# Dictionary containing the population of different age groups in California
california_population = {
'0-14': 7323000,
'15-24': 4785000,
'25-54': 18669000,
'55-64': 6937000,
'65+': 5944000
}
pop_values = california_population.values()
# Calculate the total population
total_population = sum(pop_values)
# Calculate the percentage of each age group
age_group_percentages = [(value / total_population) * 100 for value in pop_values]
print(age_group_percentages)
In this example, we have a Python dictionary containing the population of different age groups in California. We use the values() method to extract the population data and calculate the percentage of each age group in the total population.
Output:
Example#3 Processing Survey Data
# Dictionary containing survey responses on customer satisfaction (1-5 scale)
survey_responses = {
'response_1': 5,
'response_2': 3,
'response_3': 4,
'response_4': 2,
'response_5': 4
}
response_values = survey_responses.values()
# Calculate the average satisfaction score
average_score = sum(response_values) / len(response_values)
# Filter the responses with a score greater than or equal to 4
satisfied_responses = [value for value in response_values if value >= 4]
print(average_score)
print(satisfied_responses)
This example involves a Python dictionary containing survey responses on customer satisfaction. We use the values() method to extract the response values, calculate the average satisfaction score, and filter responses that have a score greater than or equal to 4.
Output:
Example#4 Comparing Values from Multiple Dictionaries
# Dictionaries containing key economic indicators for California and Texas
california_economy = {
'GDP': 3.2e12,
'unemployment_rate': 8.3,
'average_income': 71805
}
texas_economy = {
'GDP': 1.9e12,
'unemployment_rate': 6.7,
'average_income': 62947
}
california_values = california_economy.values()
texas_values = texas_economy.values()
# Compare the economic indicators
comparison = [round(california / texas, 2) for california, texas in zip(california_values, texas_values)]
print(comparison)
In this example, we have two Python dictionaries containing key economic indicators for California and Texas. We use the values() method to extract the data from each Python dictionary, and then compare the indicators to identify trends or anomalies between the two states.
Output:
Conclusion
The Python dictionary values() method is a useful function for accessing and working with the values of a dictionary. It is easy to use, flexible, and enables you to convert, filter, or manipulate the values in various ways.
You may also like to read the following articles:
- Python Program to Convert Two Lists Into a Dictionary
- How to convert Python tuple to dictionary
- How to extract all values from a dictionary in Python
- 8 ways to create Python dictionary of lists
- Python dictionary get() method [With Examples]
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.