Python program to remove each y occurrence before x in List
Last Updated :
08 May, 2023
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
test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ]
print ( "The original list is : " + str (test_list))
x, y = 6 , 4
xidx = test_list.index(x)
res = [ele for idx, ele in enumerate (test_list) if ele ! = y or (ele = = y and idx > xidx) ]
print ( "Filtered List " + str (res))
|
Output
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
test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ]
print ( "The original list is : " + str (test_list))
x, y = 6 , 4
xidx = test_list.index(x)
res = []
for idx, ele in enumerate (test_list):
if ele ! = y or (ele = = y and idx > xidx):
res.append(ele)
print ( "Filtered List " + str (res))
|
Output
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)
|
Output
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
test_list = [ 4 , 5 , 7 , 4 , 6 , 7 , 4 , 9 , 1 , 4 ]
print ( "The original list is : " + str (test_list))
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)
print ( "Filtered List " + str (a))
|
Output
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...