Python dict.values()

In this article we will discuss how to use values() function of dict class in python and then we will see several examples of the values() function.

dict.values() Syntax:

In python, the dict class provides a member function to fetch all values from dictionary i.e.

dict.values()

Parameter:

      • None

Return Value:

      • It returns a sequence containing the view of all values from the dictionary. If any value gets modified in the dictionary then it will be reflected in sequence too, because that is just a view of values.

Let’s understand with some examples,

Examples of dict.values() in python

Get all values from a dictionary in python

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# Get all values from a dictionary in python
values = word_freq.values()

print(values)

Output:

dict_values([56, 23, 43, 78, 99])

values() function returned a sequence dict_values, it contains the view of all values in the dictionary.

We can also iterate over the sequence and access each value one by one,

print('Iterate over all values')

for elem in word_freq.values():
    print(elem)

Output:

Iterate over all values
56
23
43
78
99

Effect of Dictionary modification on returned values

If we first fetch all the values of the dictionary using values() function and then modify the dictionary, then changes will be reflected in the sequence of the already fetched values too. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# Get all values from a dictionary in python
values = word_freq.values()

print('Values of dictionary: ')
print(values)

# Modify the value of a key, it will modify the values
# in already fetched sequence too
word_freq['at'] = 200

print('Values of dictionary: ')
print(values)

Output:

Values of dictionary: 
dict_values([56, 23, 43, 78, 99])
Values of dictionary: 
dict_values([56, 200, 43, 78, 99])

Here we created a sequence of values from the dictionary and then we modified the value of key ‘at’ in the dictionary. Now the value of key ‘at’ is also updated in the sequence of the values, which we got from the values() function, because values() method always returns a view of all values in the dictionary.

But now suppose we don’t want the view, what if we want a copy of all values in the dictionary?

We can do that by converting all values of the dictionary to a list,

Convert values of a dictionary to a list

To create a list of all values in the dictionary we can pass the sequence returned by values() function to the list() i.e.

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# convert all values in dictionary to a list
list_of_values = list( word_freq.values() )

print('Values of Dictionary: ')
print(list_of_values)

Output:

Values of Dictionary: 
[56, 23, 43, 78, 99]

It returned a list of all values in the dictionary. The list contains a copy of the values in the dictionary. It means if we modify the value of any key in the dictionary then it will not update the list of values. For example,

# Modify the value of a key
word_freq['at'] = 200

print('Values of Dictionary: ')
print(list_of_values)

Output:

Values of Dictionary: 
[56, 23, 43, 78, 99]

We modified the value of key ’at’, but the changes are not reflected in the already created values list, because it contains the copy of values not the view.

Get the sum of dictionary values in python

As values() function returns a sequence of values of the dictionary, so we can pass this sequence to the sum() function to get the sum of all values of the dictionary. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# python dictionary values sum
sum_of_values = sum(word_freq.values())

print('Sum of Values: ', sum_of_values)

Output:

Sum of Values:  299

Get average of dictionary values in python

To get the average of all values in the dictionary, just divide the sum of values by the size of the dictionary. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# python dictionary values average
avg_of_values = sum(word_freq.values()) / len(word_freq.values())

print('Average of values: ', avg_of_values)

Output:

Average of values:  59.8

So, this is how we can use the values() method of dict class.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top