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]
Explanation : 4 is removed as 5 – 4 = 1 < 3, hence its within distance.
Method #1 : Using list comprehension
In this, we extract only those elements which are at safe distance by magnitude K to N using comparisons using list comprehension.
Python3
# Python3 code to demonstrate working of # Remove Elements in K distance with N# Using list comprehension# initializing listtest_list = [4, 5, 9, 1, 10, 6, 13 ]# printing original listprint("The original list is : " + str(test_list))# initializing K K = 3# initializing N N = 5# checking for elements in safe zone with respect to Nres = [ele for ele in test_list if ele < N - K or ele > N + K]# printing result print("Filtered elements : " + str(res)) |
The original list is : [4, 5, 9, 1, 10, 6, 13] Filtered elements : [9, 1, 10, 13]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda
In this, task of filtering is done using filter() and lambda function.
Python3
# Python3 code to demonstrate working of # Remove Elements in K distance with N# Using filter() + lambda# initializing listtest_list = [4, 5, 9, 1, 10, 6, 13 ]# printing original listprint("The original list is : " + str(test_list))# initializing K K = 3# initializing N N = 5# checking for elements in safe zone with respect to Nres = list(filter(lambda ele : ele < N - K or ele > N + K, test_list))# printing result print("Filtered elements : " + str(res)) |
The original list is : [4, 5, 9, 1, 10, 6, 13] Filtered elements : [9, 1, 10, 13]
Method#3:Using list comprehension with if-else statement
Algorithm:
- Initialize the list to be processed.
- Initialize the value of K and N.
- Create a list comprehension to filter elements which are at a safe distance from N.
- Create another list comprehension to remove None elements from the result list.
- Print the filtered list.
Python3
# Python3 code to demonstrate working of# Remove Elements in K distance with N# Using list comprehension with if-else statement# initializing listtest_list = [4, 5, 9, 1, 10, 6, 13 ]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 3# initializing NN = 5# checking for elements in safe zone with respect to Nres = [ele if ele < N - K or ele > N + K else None for ele in test_list]res = [ele for ele in res if ele is not None]# printing resultprint("Filtered elements : " + str(res)) |
The original list is : [4, 5, 9, 1, 10, 6, 13] Filtered elements : [9, 1, 10, 13]
Time complexity:
The time complexity of this approach is O(n) because the list comprehension is being used, which has a time complexity of O(n).
Auxiliary Space:
The auxiliary space complexity of this approach is O(n) because a new list is being created to store the filtered elements.
Method 4 : using a for loop and a new list to store the filtered elements.
Here are the steps:
- Initialize the original list, test_list.
- Print the original list.
- Initialize K and N.
- Initialize an empty list to store the filtered elements, res.
- Loop through each element in the test_list.
- If the absolute difference between the current element and N is greater than K, then append it to the res list.
- Print the filtered elements, res.
Python3
# Python3 code to demonstrate working of# Remove Elements in K distance with N# Using for loop and new list# initializing listtest_list = [4, 5, 9, 1, 10, 6, 13]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 3# initializing NN = 5# initializing empty list to store filtered elementsres = []# loop through each element in test_listfor ele in test_list: # check if the absolute difference between ele and N is greater than K if abs(ele - N) > K: # if so, append the element to the res list res.append(ele)# printing resultprint("Filtered elements : " + str(res)) |
The original list is : [4, 5, 9, 1, 10, 6, 13] Filtered elements : [9, 1, 10, 13]
Time complexity: O(n), where n is the length of the original list.
Auxiliary space: O(m), where m is the number of elements that are outside the safe zone.



