Python - Extract element with relative difference greater than K
Last Updated :
13 Mar, 2023
Given a list of numbers, the task is to write a Python program to extract all numbers with differences of the next and previous elements with a current greater than K.
Input : test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5], K = 4
Output : [9, 10]
Explanation : 9 has 1 as preceding element and 2 as succeeding. 8 and 7 are its difference respectively which are greater than 4.
Input : test_list = [2, 7, 4, 1, 9, 2], K = 4
Output : [9]
Explanation : 9 has 1 as preceding element and 2 as succeeding. 8 and 7 are its difference respectively which are greater than 4.
Method #1: Using loop
In this, we check using brute force if the next or previous element has elements less than K difference and omit them. Loop is used for the iteration of all the elements.
Python3
# Python3 code to demonstrate working of
# Extract element with relative difference
# greater than K Using loop
# initializing list
test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
res = []
for idx in range(1, len(test_list)):
# using abs to get absolute difference
if abs(test_list[idx - 1] - test_list[idx]) > K\
and abs(test_list[idx + 1] - test_list[idx]) > K:
res.append(test_list[idx])
# printing result
print("The extracted difference elements : " + str(res))
OutputThe original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
This is similar to the above method, the difference only being the usage of list comprehension for availing a shorthand to solving the issue.
Python3
# Python3 code to demonstrate working of
# Extract element with relative difference
# greater than K Using list comprehension
# initializing list
test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
# using abs to get absolute difference
# list comprehension provides shorthand
res = [test_list[idx] for idx in range(
1, len(test_list)) if abs(test_list[idx - 1] - test_list[idx]) > K
and abs(test_list[idx + 1] - test_list[idx]) > K]
# printing result
print("The extracted difference elements : " + str(res))
OutputThe original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using numpy
Note: Install numpy module using command "pip install numpy"
Python3
#importing numpy
import numpy as np
# initializing list
test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
# using np.diff to get absolute difference
res = [test_list[idx] for idx in range(1, len(test_list))
if np.diff([test_list[idx - 1], test_list[idx]])[0] > K
and np.diff([test_list[idx + 1], test_list[idx]])[0] > K]
# printing result
print("The extracted difference elements : " + str(res))
Output:
The original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using filter()
- Use filter() to iterate through all the elements in the test_list except the first and the last element (since we are checking the relative difference between adjacent elements).
- The lambda function inside filter() checks whether the absolute difference between the current element and its adjacent elements is greater than K.
- The resulting elements that satisfy the condition are stored in the list res.
Python3
test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]
K = 4
res = list(filter(lambda x: abs(x - test_list[test_list.index(x) - 1]) > K and abs(x - test_list[test_list.index(x) + 1]) > K, test_list[1:-1]))
print("The extracted difference elements : " + str(res))
OutputThe extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n), for storing the result in the list res
Similar Reads
Python - Remove Tuples with difference greater than K Given Dual Tuples List, remove pairs with differences greater than K. Input : test_list = [(4, 8), (1, 7), (9, 12), (3, 12), (2, 10)], K = 6 Output : [(4, 8), (9, 12), (1, 7)] Explanation : 4 (8 - 4), 3 (12 - 9) and 6 are all not greater than 6, hence retained. Input : test_list = [(4, 8), (1, 7), (
5 min read
Python - Extract elements with Frequency greater than K Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read
Python - Extract list with difference in extreme values greater than K Given a list of lists. The task is to filter all rows whose difference in min and max values is greater than K. Examples: 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. Inp
4 min read
Python - Extract dictionaries with values sum greater than K Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 15 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. Input : te
8 min read
Python Program to remove elements that are less than K difference away in a list Given a list, perform removal of those elements whose difference is less than K from its previous element. Input : test_list = [3, 19, 5, 8, 10, 13], K = 4Â Output : [3, 8, 13, 19]Â Explanation : 5 - 3 = 2, 2<4, hence 5 is removed, similarly, 10 - 8 is 2, less than K. Input : test_list = [15, 7, 20
3 min read
Python - Remove Elements in K distance with N Given a list, remove all elements which are within K distance with N. Input : test_list = [4, 5, 9, 1, 10, 6, 13 ], K = 3, N = 5 Output : [9, 1, 10, 13] Explanation : 4 is removed as 5 - 4 = 1 < 3, hence its within distance. Input : test_list = [1, 10, 6, 13 ], K = 3, N = 5 Output : [1, 10, 13] E
5 min read