Open In App

Python – Remove Elements in K distance with N

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 list
test_list = [4, 5, 9, 1, 10, 6, 13 ]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# initializing N
N = 5
 
# checking for elements in safe zone with respect to N
res = [ele for ele in test_list if ele < N - K or ele > N + K]
 
# printing result
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




# Python3 code to demonstrate working of
# Remove Elements in K distance with N
# Using filter() + lambda
 
# initializing list
test_list = [4, 5, 9, 1, 10, 6, 13 ]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# initializing N
N = 5
 
# checking for elements in safe zone with respect to N
res = list(filter(lambda ele : ele < N - K or ele > N + K, test_list))
 
# printing result
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:

  1. Initialize the list to be processed.
  2. Initialize the value of K and N.
  3. Create a list comprehension to filter elements which are at a safe distance from N.
  4. Create another list comprehension to remove None elements from the result list.
  5. 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 list
test_list = [4, 5, 9, 1, 10, 6, 13 ]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# initializing N
N = 5
 
# checking for elements in safe zone with respect to N
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]
 
# printing result
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:

  1. Initialize the original list, test_list.
  2. Print the original list.
  3. Initialize K and N.
  4. Initialize an empty list to store the filtered elements, res.
  5. Loop through each element in the test_list.
  6. If the absolute difference between the current element and N is greater than K, then append it to the res list.
  7. 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 list
test_list = [4, 5, 9, 1, 10, 6, 13]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# initializing N
N = 5
 
# initializing empty list to store filtered elements
res = []
 
# loop through each element in test_list
for 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 result
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
Previous
Next
Share your thoughts in the comments

Similar Reads