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
# 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))
OutputThe 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))
OutputThe 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)
OutputFiltered 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))
OutputThe 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
Similar Reads
Python program to Test if all y occur after x in List Given a List, test if all occurrences of y are after the occurrence of x in the list. Input : test_list = [4, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 Output : True Explanation : All occurrences of 2 are after 6, hence true. Input : test_list = [4, 2, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 Output : False Explan
4 min read
Python program to remove Nth occurrence of the given word Given a list of words in Python, the task is to remove the Nth occurrence of the given word in that list. Examples:Â Input: list - ["geeks", "for", "geeks"] word = geeks, N = 2 Output: list - ["geeks", "for"]Input: list - ["can", "you", "can", "a", "can" "?"] word = can, N = 1 Output: list - ["you",
8 min read
Python | Remove all occurrences in nested list The task of removing an element generally doesn't pose any challenge, but sometimes, we may have a more complex problem than just removing a single element or performing removal in just a normal list. The problem can be removing all occurrences of the nested list. Let's discuss certain ways in which
5 min read
Python program to reverse a range in list Reversing a specific range within a list is a common task in Python programming that can be quite useful, when data needs to be rearranged. In this article, we will explore various approaches to reverse a given range within a list. We'll cover both simple and optimized methods. One of the simplest w
3 min read
Python program to find the Decreasing point in List Given a list, get the index of element where the list shows the first negative trend, i.e first point where the next element < current element. If not found return -1. Input : test_list = [3, 6, 8, 9, 12, 5, 18, 1] Output : 4 Explanation : At 12 -> 5, first decreasing point occurs. Input : tes
4 min read