Given a list of lists. The task is to filter all rows whose difference in min and max values is greater than K.
Input : test_list = [[13, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]], K = 5
Output : [[9, 1, 2], [1, 10, 2], [13, 5, 1]]
Explanation : 8, 9, 12 are differences, greater than K.
Input : test_list = [[13, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]], K = 15
Output : []
Explanation : No list with diff > K.
In this, we perform task of iteration using list comprehension and the task of checking is done using the comparison operator. Values are computed using max() and min().
The original list is : [[3, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]] Filtered rows : [[9, 1, 2], [1, 10, 2]]
In this, we perform task of filtering using filter() and lambda, rest min() and max(), are used to get extreme values difference.
The original list is : [[3, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]] Filtered rows : [[9, 1, 2], [1, 10, 2]]
OutputThe original list is : [[3, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]]
Filtered rows : [[9, 1, 2], [1, 10, 2]]