Dictionary method

dict.values()

Returns a live view of all the values in a dictionary, in key order. Handy for totals, maximums and searching values.

BeginnerEvery example below runs in your browser. Change the code and press Run.

dict.values()

values() gives you every value stored in a dictionary, without the keys. They come out in the same order as the keys.

It is what you reach for when you need a total, an average, the largest value, or to check whether some value appears at all.

Like dict.keys(), the result is a view that follows the dictionary as it changes, and cannot be indexed. Unlike keys, values can repeat, so a values view does not support set operations.

Return value

A dict_values view of the dictionary's values.

Examples

1Total and average

Pythoneditable
expenses = {"rent": 900, "food": 350, "travel": 120}
print(list(expenses.values()))
total = sum(expenses.values())
print("Total:", total)
print("Average:", total / len(expenses))
Output
[900, 350, 120]
Total: 1370
Average: 456.6666666666667

2Search the values

Pythoneditable
roles = {"Ana": "admin", "Ben": "editor", "Chloe": "viewer"}
print("admin" in roles.values())
print("owner" in roles.values())
print("admin" in roles)
Output
True
False
False

in roles alone checks the keys, not the values.

3Highest value, and who has it

Pythoneditable
sales = {"Lena": 4200, "Omar": 5100, "Yuki": 3900}
print(max(sales.values()))
best = max(sales, key=sales.get)
print(best)
Output
5100
Omar

max(sales.values()) gives the number only. max(sales, key=sales.get) gives the key that holds it.

4Change values in place

If the values are lists, you can change them through the view:

Pythoneditable
teams = {"red": ["Ana"], "blue": ["Ben", "Dan"]}
for members in teams.values():
    members.append("Coach")
print(teams)
Output
{'red': ['Ana', 'Coach'], 'blue': ['Ben', 'Dan', 'Coach']}

Common mistakes

Expecting a list

d.values()[0] raises a TypeError. Wrap the view in list() when you need positions.

Pythoneditable
temps = {"Mon": 18, "Tue": 21}
vals = list(temps.values())
print(vals[0])
Output
18

Reassigning the loop variable

for v in d.values(): v = v * 2 changes only the variable v, not the dictionary. Loop over items() and assign by key.

Pythoneditable
prices = {"tea": 2, "cake": 3}
for v in prices.values():
    v = v * 2
print(prices)
for k, v in prices.items():
    prices[k] = v * 2
print(prices)
Output
{'tea': 2, 'cake': 3}
{'tea': 4, 'cake': 6}

Questions people ask

How do I sum the values of a dictionary?

sum(d.values()).

How do I find the key for a given value?

Loop over dict.items(): [k for k, v in d.items() if v == target]. Several keys may share one value.

How do I get unique values?

set(d.values()), then sorted(...) if you want a fixed order to print.

Spotted a mistake on this page? Tell us