Given a list, remove all the occurrence of y before element x in list.
Input : test_list = [4, 5, 7, 4, 6, 7, 4, 9, 1, 4], x, y = 6, 4
Output : [5, 7, 6, 7, 4, 9, 1, 4]
Explanation : All occurrence of 4 before 6 are removed.Input : test_list = [4, 5, 7, 4, 6, 7, 4, 9, 1, 4], x, y = 6, 7
Output : [4, 5, 4, 6, 7, 4, 9, 1, 4]
Explanation : All occurrence of 7 before 6 are removed.
Method #1 : Using list comprehension + index()
In this, we get the index of x using index(), and check for y before x, if present that is excluded from the result list. The iteration and comparison are performed using list comparison.
Python3
# Python3 code to demonstrate working of # Remove each y occurrence before x in List # Using list comprehension + index() # initializing list test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ] # printing original lists print ( "The original list is : " + str (test_list)) # initializing x and y x, y = 6 , 4 # getting index using index() xidx = test_list.index(x) # retain all values other than y, and y if its index greater than x index res = [ele for idx, ele in enumerate (test_list) if ele ! = y or (ele = = y and idx > xidx) ] # printing result print ( "Filtered List " + str (res)) |
The original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4] Filtered List [5, 7, 6, 7, 4, 9, 1, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using loop + index()
This task of filtering is done using comparison operators in the loop, index(), is used to get the index of x in list.
Python3
# Python3 code to demonstrate working of # Remove each y occurrence before x in List # Using loop + index() # initializing list test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ] # printing original lists print ( "The original list is : " + str (test_list)) # initializing x and y x, y = 6 , 4 # getting index using index() xidx = test_list.index(x) # filtering using comparison operators res = [] for idx, ele in enumerate (test_list): if ele ! = y or (ele = = y and idx > xidx): res.append(ele) # printing result print ( "Filtered List " + str (res)) |
The original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4] Filtered List [5, 7, 6, 7, 4, 9, 1, 4]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 3: uses list slicing
step-by-step approach
- Initialize the input list test_list with the values [4, 5, 7, 4, 6, 7, 4, 9, 1, 4].
- Initialize the variables x and y with the values 6 and 4, respectively.
- Get the index of the first occurrence of x in the list using the index() method and store it in the variable xidx.
- Use list slicing to create a new list that contains all the elements of test_list before the first occurrence of x, and add to it a list comprehension that filters out all occurrences of y before the first occurrence of x. Store the resulting list in the variable res.
- Print the filtered list res using the print() function.
- The code first finds the index of the first occurrence of x in the
Python3
test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ] x, y = 6 , 4 xidx = test_list.index(x) res = test_list[:xidx] + [ele for ele in test_list[xidx:] if ele ! = y] print ( "Filtered List: " , res) |
Filtered List: [4, 5, 7, 4, 6, 7, 9, 1]
Time complexity: O(n) where n is the length of the input list.
Auxiliary space complexity: O(n) where n is the length of the input list due to the creation of the new list res.
Method 4 : Using slicing , remove() , index() , extend() methods
Approach
- Find the index of x using index()
- Slice the test_list from beginning to z and store in a, slice the list from z to end and store in b
- Remove all y in a using remove() and while loop
- Append all elements of b to a using extend()
- Display a
Python3
# Python3 code to demonstrate working of # Remove each y occurrence before x in List # initializing list test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ] # printing original lists print ( "The original list is : " + str (test_list)) # initializing x and y x, y = 6 , 4 z = test_list.index(x) a = test_list[:z] b = test_list[z:] while y in a: a.remove(y) a.extend(b) # printing result print ( "Filtered List " + str (a)) |
The original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4] Filtered List [5, 7, 6, 7, 4, 9, 1, 4]
Time complexity: O(n) where n is the length of the input list
Auxiliary space: O(n) where n is the length of list a