find the smallest key in a map in python

To find the smallest key in a Map (Dictionary) in Python, we can make use of the min() function on the keys of the dictionary. Here's an example:

main.py
my_map = {"apple": 1, "banana": 2, "cherry": 3, "date": 4}

smallest_key = min(my_map.keys())

print(smallest_key)  # Output: "apple"
134 chars
6 lines

In the code above, we use the min() function on the keys of the Map (my_map.keys()) to get the smallest key. Finally, we print the smallest key which should output "apple".

gistlibby LogSnag