Suppose we have a python dictionary that stores grades with respect to roll numbers. dict = {1:90,2:40,3:98}. If we use min(dict) then the output will be 1. How can we find the least marks out of the group(in this case 40). Also can we find the index of the returned value?
Asked
Active
Viewed 5,879 times
1 Answers
6
Use the key
argument of min
:
d = {1: 90, 2: 40, 3: 98}
lowest = min(d, key=d.get)
print(lowest, d[lowest])
# 2 40
Another way:
lowest = min(d.items(), key=lambda x: x[1])
print(lowest)
# (2, 40)

iz_
- 15,923
- 3
- 25
- 40
-
1Please try to tone down the duplicate answering. If you see basic questions like these, they've likely already been asked. Search out duplicates before answering. – cs95 Jan 12 '19 at 07:34