

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Display records on the basis of key-value pairs in MySQL
- How to find the element from a Python list with a maximum value?
- Create list of numbers with given range in Python
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- Path With Maximum Minimum Value in Python
- Python - Return the maximum value of the Pandas Index
- Python – Maximum in Row Range
- Display the minimum and maximum value of primitive data types in Java
- Maximum XOR value of a pair from a range in C++
- Display all the numbers from a range of start and end value in JavaScript?
- Python Pandas - Display the value of the stop parameter of RangeIndex
Advertisements