max( { 'a': 5, 'b': 6, 'c': 7, 'd': 4,})
gives 'd'
Why isn't it giving 'c'
?
max( { 'a': 5, 'b': 6, 'c': 7, 'd': 4,})
gives 'd'
Why isn't it giving 'c'
?
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.
Hope this helps! diction1={ 'a': 5, 'b': 6, 'c': 7, 'd': 4,} max(diction1.values())