Python – Remove Elements in K distance with N
Last Updated :
22 Apr, 2023
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
test_list = [ 4 , 5 , 9 , 1 , 10 , 6 , 13 ]
print ( "The original list is : " + str (test_list))
K = 3
N = 5
res = [ele for ele in test_list if ele < N - K or ele > N + K]
print ( "Filtered elements : " + str (res))
|
Output
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
test_list = [ 4 , 5 , 9 , 1 , 10 , 6 , 13 ]
print ( "The original list is : " + str (test_list))
K = 3
N = 5
res = list ( filter ( lambda ele : ele < N - K or ele > N + K, test_list))
print ( "Filtered elements : " + str (res))
|
Output
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
test_list = [ 4 , 5 , 9 , 1 , 10 , 6 , 13 ]
print ( "The original list is : " + str (test_list))
K = 3
N = 5
res = [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 ]
print ( "Filtered elements : " + str (res))
|
Output
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
test_list = [ 4 , 5 , 9 , 1 , 10 , 6 , 13 ]
print ( "The original list is : " + str (test_list))
K = 3
N = 5
res = []
for ele in test_list:
if abs (ele - N) > K:
res.append(ele)
print ( "Filtered elements : " + str (res))
|
Output
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...