- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to remove each y occurrence before x in List
When it is required to remove every ‘y’ occurrence before ‘x’ in a list, a list comprehension along with the ‘index’ method are used.
Below is a demonstration of the same
my_list = [4, 45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8] print("The list is :") print(my_list) a, b = 8, 4 index_a = my_list.index(a) my_result = [ele for index, ele in enumerate(my_list) if ele != b or (ele == b and index > index_a) ] print("The resultant list is ") print(my_result)
The list is : [4, 45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8] The resultant list is [45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8]
A list is defined and is displayed on the console.
Two variables are assigned integer values.
The index of one of the variables is obtained.
This is assigned to a variable.
A list comprehension is used to iterate through the list, using ‘enumerate’.
A condition is placed to check if the element is equal to (or not) the second variable.
The result of this operation is assigned to a variable.
This is displayed as the output on the console.
- Program to count number of flipping required to make all x before y in Python
- Python program to test if all y occur after x in List
- Python Scatter Plot with Multiple Y values for each X
- Python program to find occurrence to each character in given string
- Program to remove last occurrence of a given target from a linked list in Python
- Program to remove string characters which have occurred before in Python
- C# program to remove the first occurrence of a node in a Linked List
- Java program to check occurrence of each character in String
- Java program to check occurrence of each vowel in String
- How to remove X or Y labels from a Seaborn heatmap?
- Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat
- Find larger of x^y and y^x in C++
- Program to check whether given list of blocks are symmetric over x = y line or not in python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++