Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the largest key in a dictionary

I have a dictionary with keys that are ints. I would like to get the largest key. I don't keep track of keys so they might be consecutive (e.g. 1,2,3,4,5,6) but might skip (1,3,4,5) although I doubt that makes any difference.

Do I just use a binary search or is there a method? As far as I see you can hardly beat binary search for such a simple task - maybe you can halve it.

like image 591
s5s Avatar asked Nov 29 '11 17:11

s5s


People also ask

How do you find the largest key in a dictionary?

If you have a dict with string-integer mappings, you can use the max method on the dictionary's item pairs to get the largest value.

How do you find the key length in a dictionary?

To check the length of a Python dictionary, we can easily use Python's built-in function len(). This function gives the number of lengths which is available in the dictionary in the form of key-value pairs. Len() function always return the number of iterable items given in the dictionary.

What is .get in Python?

Python Dictionary get() Method The get() method returns the value of the item with the specified key.


1 Answers

If you have LINQ available, you should be able to do:

myDictionary.Keys.Max(); 
like image 95
Ry- Avatar answered Sep 22 '22 14:09

Ry-