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
Similar Reads
Python program to Occurrences of i before first j in list
Given a list, the task is to write a Python program to count the occurrences of ith element before the first occurrence of jth element. Examples: Input : test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9], i, j = 4, 8 Output : 3 Explanation : 4 occurs 3 times before 8 occurs. Input : test_list = [4, 5
6 min read
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 For Removing All Occurrences Of Duplicates From A Sorted Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty Li
3 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
4 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
Last Occurrence of Some Element in a List - Python
We are given a list we need to find last occurrence of some elements in list. For example, w = ["apple", "banana", "orange", "apple", "grape"] we need to find last occurrence of string 'apple' so output will be 3 in this case. Using rindex()rindex() method in Python returns the index of the last occ
3 min read
Python Program For Deleting Last Occurrence Of An Item From Linked List
Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next special pointer to remove the required node from the linked list. C/C++ Cod
6 min read
Remove Negative Elements in List-Python
The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read