0

Here is the code:

max({1: "one", 2: "two", 3: "three"})

It returns 3.

I wonder what is the rule here? Function max() operates on a dictionary, how does the comparison work on a dictionary? So the item comparison is to compare their keys?

Where can I find the explanation in the language spec?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
my_question
  • 3,075
  • 2
  • 27
  • 44
  • Yes, it is maximum of keys. If you simply iterate over a dictionary, you are iterating over its keys. – Austin Feb 22 '20 at 05:27
  • Iterating a dictionary yields its keys. `3` is the highest key. – Klaus D. Feb 22 '20 at 05:27
  • 1
    Instances of `dict` have the method `__iter__`. That allows you to use `for k in mydict`. That's what makes `dict` iterable. That's what `max` uses to do its magic. – Mateen Ulhaq Feb 22 '20 at 05:29

0 Answers0