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.
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.
OutputThe original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4]
Filtered List [5, 7, 6, 7, 4, 9, 1, 4]
This task of filtering is done using comparison operators in the loop, index(), is used to get the index of x in list.
OutputThe original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4]
Filtered List [5, 7, 6, 7, 4, 9, 1, 4]
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.
OutputThe original list is : [4, 5, 7, 4, 6, 7, 4, 9, 1, 4]
Filtered List [5, 7, 6, 7, 4, 9, 1, 4]