0

How can I get the largest key in a dictionary which may have mixed data types? For example:

myDict = {1: 'foo', 2: 'bar', 3: None, 4: 1000}

I want to get the largest key which is 4. I tried max(myDict) which does work, but is this always reliable? Or should I use max(myDict.keys()) instead?

Elliott B
  • 658
  • 3
  • 22

1 Answers1

0

The options you gave (max(myDict) and max(myDict.keys())) are equivalent, because max internally tries to iterate over the thing you pass to it. Iteration over a Python dictionary yields its keys, so, no real difference.

Josh Karpel
  • 1,989
  • 2
  • 7
  • 15