
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to get maximum of each key Dictionary List
When it is required to get the maximum of each key in a list of dictionary elements, a simple iteration is used.
Example
Below is a demonstration of the same
my_list = [{"Hi": 18, "there": 13, "Will": 89}, {"Hi": 53, "there": 190, "Will": 87}] print("The list is : ") print(my_list) my_result = {} for elem in my_list: for key, val in elem.items(): if key in my_result: my_result[key] = max(my_result[key], val) else: my_result[key] = val print("The result is : ") print(my_result)
Output
The list is : [{'Will': 89, 'there': 13, 'Hi': 18}, {'Will': 87, 'there': 190, 'Hi': 53}] The result is : {'Will': 89, 'there': 190, 'Hi': 53}
Explanation
A list of dictionary is defined and is displayed on the console.
An empty dictionary is defined.
The list is iterated over, and the elements are accessed.
If the key is present in the previously defined dictionary, the maximum of the key and the value is determined, and stored in the ‘key’ index of the dictionary.
Otherwise, the value is stored in the ‘key’ index of the dictionary.
This is displayed as the output on the console.
- Related Articles
- Get key with maximum value in Dictionary in Python
- Convert key-values list to flat dictionary in Python
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Sort Dictionary key and values List in Python
- Get key from value in Dictionary in Python
- Python program to get the indices of each element of one list in another list
- C# program to get the List of keys from a Dictionary
- Python Program to print key value pairs in a dictionary
- Python – Display the key of list value with maximum range
- Python – Concatenate Tuple to Dictionary Key
- Python program to convert a list of tuples into Dictionary
- Program to get maximum value of power of a list by rearranging elements in Python
- Get dictionary keys as a list in Python
- Python program to Convert Matrix to Dictionary Value List
- Python - Filter dictionary key based on the values in selective list

Advertisements