Sum, Max and Min commands in Dictionaries

Perhaps I’d find the answer later in the course, but I still want to ask why I couldn’t use max(), min() and sum() commands when working with dictionaries.

You can use these methods with dictionary but it will give you results based on dictionary keys not values

For Example:

stats = {0:1000, 1:30000, 2: 100, 3:3000}
print("Sum: {}".format(sum(stats)))
print("Max: {}".format(max(stats)))
print("Min: {}".format(min(stats)))

Sum: 6
Max: 3
Min: 0

To get results by values you can use values() method that will returns all values of dictionary.

print("\nSum, Max and Min for Dictionary values")
print("Sum: {}".format(sum(stats.values())))
print("Max: {}".format(max(stats.values())))
print("Min: {}".format(min(stats.values())))


Sum, Max and Min for Dictionary values
Sum: 34100
Max: 30000
Min: 100
1 Like

Interesting… So I am going to learn this later in the course right? Which module will I learn this? I am currently refreshing my knowledge on Python fundamentals before proceeding to the intermediate practice.

Don’t know In which module it is or might be not in any. But you will learn it as do progress by yourself.