

- 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 – Row with Minimum difference in extreme values
When it is required to get the row with minimum difference in extreme values, list comprehension, the ‘min’ method and ‘max’ methods are used.
Example
Below is a demonstration of the same
my_list = [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] print("The list is : ") print(my_list) my_min_val = min([max(elem) - min(elem) for elem in my_list]) my_result = [elem for elem in my_list if max(elem) - min(elem) == my_min_val] print("The result is : ") print(my_result)
Output
The list is : [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] The result is : [[1, 22]]
Explanation
A list of list is defined and is displayed on the console.
The list is iterated over and the difference between maximum and minimum elements is determined.
The minimum of this is obtained and assigned to a variable.
A list comprehension is used to iterate through the list and see if the difference between the maximum and minimum element is equal to the previously defined variable.
This is assigned to a variable.
This is displayed as output on the console.
- Related Questions & Answers
- Python – Extract list with difference in extreme values greater than K
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Insert row with only default values in MySQL
- Get minimum difference in Tuple pair in Python
- Minimum Cost Tree From Leaf Values in Python
- How to multiply corresponding row values in a matrix with single row matrix in R?
- Program to find minimum absolute sum difference in Python
- Minimum Time Difference in C++
- Path With Maximum Minimum Value in Python
- Connecting Cities With Minimum Cost in Python
- How to get the values of a particular row in a table in Selenium with python?
- How to fill a data.table row with missing values in R?
- How to multiply corresponding row values in a data.table object with single row data.table object in R?
- Excluding extreme elements from average calculation in JavaScript
- Pairing an array from extreme ends in JavaScript
Advertisements