Python | Minimum value keys in Dictionary

Python | Minimum value keys in Dictionary

To find the keys associated with the minimum values in a dictionary, you can use Python's built-in functions and comprehensions. Here are the steps:

  1. Find the Minimum Value: Use the min() function to determine the minimum value in the dictionary.

  2. Identify Keys with Minimum Value: Use a list comprehension to create a list of keys associated with the minimum value.

Here's an example:

# Sample dictionary data = { 'a': 3, 'b': 1, 'c': 4, 'd': 1, 'e': 5 } # Find the minimum value in the dictionary min_value = min(data.values()) # Get keys with the minimum value min_keys = [key for key, value in data.items() if value == min_value] print(min_keys) # Outputs: ['b', 'd'] 

In this example, both 'b' and 'd' have the minimum value of 1, so the list ['b', 'd'] is returned.


More Tags

ipv4 pinvoke sql-server-2014-express popen angular-material-table netstat ejb-3.0 digital-persona-sdk getfiles ios7

More Programming Guides

Other Guides

More Programming Examples