- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 – Extract list with difference in extreme values greater than K
When it is required to extract list with difference in extreme values greater than K, a list comprehension and the ‘min’ and ‘max’ methods are used.
Below is a demonstration of the same −
Example
my_list = [[13, 52, 11], [94, 12, 21], [23, 45, 23], [11, 16, 21]] print("The list is :") print(my_list) key = 40 my_result = [element for element in my_list if max(element) - min(element) > key] print("The result is :") print(my_result)
Output
The list is : [[13, 52, 11], [94, 12, 21], [23, 45, 23], [11, 16, 21]] The result is : [[13, 52, 11], [94, 12, 21]]
Explanation
A list is defined and displayed on the console.
The value for K is defined.
A list comprehension is used to iterate over the list, and the difference of minimum and maximum of the element is compared with the key.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python – Extract dictionaries with values sum greater than K
- Python - Number of values greater than K in list
- Python – Remove Tuples with difference greater than K
- Python – Row with Minimum difference in extreme values
- Python – Remove characters greater than K
- Find smallest element greater than K in Python
- Python Indices of numbers greater than K
- Python – Average of digit greater than K
- Python – Filter Tuples Product greater than K
- Python - Consecutive Ranges of K greater than N
- Count subarrays with all elements greater than K in C++
- Python – Extract element from a list succeeded by K
- Python - Get the Index of first element greater than K
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Python Program to remove elements that are less than K difference away in a list

Advertisements