Sunday, November 17, 2024
Google search engine
HomeLanguagesPython program to remove each y occurrence before x in List

Python program to remove each y occurrence before x in List

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))


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




# 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))


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

  1. Initialize the input list test_list with the values [4, 5, 7, 4, 6, 7, 4, 9, 1, 4].
  2. Initialize the variables x and y with the values 6 and 4, respectively.
  3. Get the index of the first occurrence of x in the list using the index() method and store it in the variable xidx.
  4. 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.
  5. Print the filtered list res using the print() function.
  6. 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

  1. Find the index of x using index()
  2. Slice the test_list from beginning to z and store in a, slice the list from z to end and store in b
  3. Remove all y in a using remove() and while loop
  4. Append all elements of b to a using extend()
  5. 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))


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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments