
- 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 – Display the key of list value with maximum range
When it is required to display the key of list value with maximum range, a simple iteration is used.
Example
Below is a demonstration of the same −
my_dict = {"pyt" : [26, 12, 34, 21], "fun" : [41, 27,43, 53, 18], "learning" : [21, 30, 29, 13]} print("The dictionary is :") print(my_dict) max_result = 0 for sub, values in my_dict.items(): max_result = max(max_result, max(values) - min(values)) if max_result == max(values) - min(values): result = sub print("The result is :") print(result)
Output
The dictionary is : {'pyt': [26, 12, 34, 21], 'fun': [41, 27, 43, 53, 18], 'learning': [21, 30, 29, 13]} The result is : fun
Explanation
A dictionary is defined and displayed on the console.
A variable is initialized to 0.
The dictionary elements are iterated over, and the ‘max’ method is used to get the maximum of the difference between the ‘max’ and ‘min’ elements and the previously determined maximum value.
If the maximum element is equal to difference between the ‘max’ and ‘min’ elements, the element is considered as result.
This is the output that is displayed on the console.
- Related Articles
- Get key with maximum value in Dictionary in Python
- Python program to get maximum of each key Dictionary List
- Get first element with maximum value in list of tuples in Python
- Find the sublist with maximum value in given nested list in Python
- How to find the element from a Python list with a maximum value?
- Create list of numbers with given range in Python
- Python Program to Alternate list elements as key-value pairs
- Python – Maximum in Row Range
- Display records on the basis of key-value pairs in MySQL
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- Path With Maximum Minimum Value in Python
- Maximum value in record list as tuple attribute in Python
- Maximum XOR value of a pair from a range in C++
- Display the minimum and maximum value of primitive data types in Java
- Assign range of elements to List in Python

Advertisements