dict.values()

Returns a view of the dictionary values, ready for loops, sums, and lookups.

Returns
A dict_values view object.
Changes the original
No
On this page

Example

#
Python
scores = {"Emma": 92, "Jack": 78, "Mia": 85}

print(scores.values())
print(sum(scores.values()))
print(max(scores.values()))
print(78 in scores.values())
Resultsaved result, you can check it
dict_values([92, 78, 85])
255
92
True

Definition and usage

#

The values() method returns a view of all the dictionary values in the order their keys were added. Like the keys view, it shows the content live, so it reflects every later change to the dictionary.

It's most often passed to functions that process a whole collection: sum(), max(), min(), sorted(), or any(). That lets you add up every student's points, for example, in a single line.

Unlike keys, values can repeat and don't need to be immutable, so the values view does not support set operations. Checking x in dict.values() works, but it scans the values one by one, so it's much slower than looking up a key on large dictionaries.

Syntax

#
Syntax
dict.values()

More examples

#
Average and unique values
Python
levels = {"Emma": 3, "Jack": 2, "Mia": 3, "Ivy": 1}

average = sum(levels.values()) / len(levels)
print(f"Average level: {average:.2f}")
print(sorted(set(levels.values())))
Resultsaved result, you can check it
Average level: 2.25
[1, 2, 3]
Who has the top score?
Python
scores = {"Emma": 92, "Jack": 78, "Mia": 85}

print(max(scores.values()))
print(max(scores, key=scores.get))
Resultsaved result, you can check it
92
Emma

Good practices

#
  • The values view alone does not say which key a given value belongs to. When you need both, use items() or max(dict, key=dict.get).
  • If you often search by value, build a reversed dictionary where the values become keys. Lookups are then instant.

Related entries

#

Spotted a mistake or missing an example? Write to us.