Problem Formulation and Solution Overview
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
You are in the market for a car and have narrowed the search to a few possibilities. Your final choice will be based on the vehicle possessing the maximum MPG. A list of these cars was provided to you in a Dictionary key:value format.
import operator
Method 1: Use dict.values() and max() to display max value
Use dict.values() and max() to determine and return the maximum value in a Dictionary using max(dict.values()).
cars_dict = {'Malibu': 18, 'Skylark': 16, 'Satellite': 19,
'Torino': 27, 'Challenge': 20}
car_values = cars_dict.values()
max_value = max(car_values)
print(max_value)This code declares a Dictionary containing five (5) key:value pairs of cars and associated MPG. The values from each pair are extracted and saved to car_values.
The max() function is called and passed car_values to determine and return the maximum value. The result saves to max_value and is output to the terminal.
Output
27 |
Method 2: Use list and max() to display max value
Use List and max() to determine and return the maximum value with max(list(cars_dict.values())).
Here’s an example:
cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenge': 20}
max_val = max(list(cars_dict.values()))
print(max_val) This code converts a Dictionary into a List, extracting values from the key:value pairs and max() to determine and return the maximum value.
The result saves to max_value and is output to the terminal.
Output
27 |
Method 3: Use dict.keys() and dict.values() to display max value and key
This example uses dict.keys() and dict.values() to determine and return the maximum value and associated key.
cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenger': 20}
sort_dict = sorted(cars_dict.items(), key=operator.itemgetter(1), reverse=True)
print(f'{sort_dict[0][0]}:\t{sort_dict[0][1]}')
This code declares a Dictionary containing five (5) key:value pairs of cars and associated MPG.
Then dict.items() and operator.itemgetter() sort the Dictionary key: value pairs in descending order. The maximum key:value pair is set as the first element.
The formatted result is output to the terminal.
Output
Torino: 27 |
Method 4: Use dict.get and max() to display max value and key
Use dict.get and max() to determine and return the maximum value and associated key with max(dict, key=cars_dict.get).
cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenger': 20}
max_val = max(cars_dict, key=cars_dict.get)
print(max_val, cars_dict[max_val])This code declares a Dictionary containing five (5) key:value pairs of cars and associated MPG.
Then max is called passing the dictionary and retrieving the key:value pairs, which save to max_val.
The print statement determines the maximum value and outputs the appropriate key:value pair to the terminal.
Output
Torino 27 |
Summary
These four (4) methods of finding the maximum value in a Dictionary should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!