Python dictionary values() method [With Examples]

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:

Python dictionary values method
Python dictionary values method

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:

Python dictionary values method example
Python dictionary values method example

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:

Dictionary values method in Python
Dictionary values method in Python

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:

Dictionary values method in Python example
Dictionary values method in Python example

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: