Python Dictionary values() Method
dict.values() returns a live view of all the values stored in a dictionary, in insertion order, ready to loop over or pass to sum(), max(), min() or list().Use values() when the keys do not matter for the task: adding up prices, finding the highest score, or checking whether any value matches something. It is one of the three view methods of a dictionary, next to keys() and items(), and part of the wider set of dictionary methods.
The result is a dict_values view. It updates automatically when the dictionary changes and, unlike keys(), it is not set-like because values may repeat and may be unhashable.
| Syntax | my_dict.values() |
|---|---|
| Parameters | None |
| Returns | A dict_values view of the values |
| Order | Insertion order (guaranteed since Python 3.7) |
| Time complexity | O(1) to create; in on values is O(n) |
How dict.values() works
The view lists each stored value once per key, so duplicates appear as many times as they occur. You can loop over it, call len() and use in, but you cannot index it. Since values are not hashed for lookup, x in d.values() has to scan them one by one, so it is slower than a key lookup on large dictionaries.
marks = {"asha": 82, "ravi": 75, "meera": 82}
print(marks.values())
print(len(marks.values()))
print(82 in marks.values())Summary functions on dictionary values
Because values() is iterable, the built-in aggregate functions work on it directly: sum(d.values()), max(d.values()), min(d.values()), and sorted(d.values()). These answer the question "what is the total or the biggest value" but not "whose value is it". To get the key that goes with the maximum, use max(d, key=d.get) or see sorting a dictionary by value.
values() compared with keys() and items()
Examples you can run
Press Run, then edit the code and run it again.
Total of all prices in a cart
sum() accepts the view directly, so no loop is needed.
cart = {"pen": 3, "notebook": 45, "ruler": 10}
print(sum(cart.values()))
print(max(cart.values()))Expected output58
45
Find the key with the highest value
values() gives the number; max(..., key=scores.get) gives the name that owns it.
scores = {"asha": 82, "ravi": 75, "meera": 91}
best = max(scores.values())
print(best)
print(max(scores, key=scores.get))Expected output91
meera
Check whether any value matches
in on values() tests values, not keys.
status = {"task1": "done", "task2": "pending"}
print("pending" in status.values())
print("pending" in status)Expected outputTrue
False
Common mistakes
Expecting a list from values()
✗ Wrong
d = {"a": 1, "b": 2}
print(d.values()[0])✓ Right
d = {"a": 1, "b": 2}
print(list(d.values())[0])Wrap the view in list() when you need indexing or slicing.
Testing a value with the dictionary itself
✗ Wrong
d = {"a": 1}
print(1 in d)✓ Right
d = {"a": 1}
print(1 in d.values())in d checks keys. Use 1 in d.values() to check values.
Python Dictionary values() Method: questions people ask
How do I get all the values of a dictionary in Python?
Call my_dict.values(), or list(my_dict.values()) if you need a list. The values come out in insertion order.
How do I sum the values in a dictionary?
Use sum(my_dict.values()). This works when all values are numbers.
Can I get a key from a value in Python?
Not directly, because dictionaries look up by key. Loop over items() and compare the values, for example [k for k, v in d.items() if v == target].
Keep learning
Step-by-step lessons:
Python DictionariesPython Dictionary Methods
Related:
Python Dictionary keys() MethodPython Dictionary items() MethodHow to Sort a Dictionary by Value in PythonHow to Loop Through a Dictionary in Python
More in dictionary methods:
Python Dictionary get() MethodPython Dictionary items() MethodPython Dictionary keys() MethodPython Dictionary update() MethodPython Dictionary pop() MethodPython Dictionary popitem() MethodPython Dictionary setdefault() MethodPython Dictionary clear() Method See all →