0
max( { 'a': 5, 'b': 6, 'c': 7, 'd': 4,})

gives 'd'

Why isn't it giving 'c'?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

2 Answers2

2

The max function takes an iterable as a parameter, so when you pass a dict to it, the dict is iterated over as an iterable, rather than a mapping. And when used as an iterable, a dict simply returns an iterator over the keys of the dict, so given a dict d, max(d) really is equivalent to max(d.keys()), which is why 'd' is returned in your example, as it has the highest value in lexicographical order among the keys of the dict.

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Hope this helps! diction1={ 'a': 5, 'b': 6, 'c': 7, 'd': 4,} max(diction1.values())