Removal of elements in list can be performed using many inbuilt functions. Removing all or just a single occurrence removal both functions are present in Python library. This article discusses to remove just the first K occurrences of elements matching particular condition.
Method #1 : Naive Method
We can append the elements that are matching condition after K occurrences of elements have been done and hence would perform the task similar to the removal.
# Python3 code to demonstrate # to remove first K elements matching condition # using Naive Method # initializing list test_list = [3, 5, 1, 6, 7, 9, 8, 5] # printing original list print ("The original list is : " + str(test_list)) # using Naive Method # to remove first K elements matching condition # removes first 4 odd occurrences counter = 1res = [] for i in test_list: if counter > 4 or not (i % 2 != 0): res.append(i) else: counter += 1 # printing result print ("The filtered list is : " + str(res)) |
The original list is : [3, 5, 1, 6, 7, 9, 8, 5] The filtered list is : [6, 9, 8, 5]
Method #2 : Using itertools.filterfalse() + itertools.count()
This is different and elegant way to perform this particular task. It filters out all the numbers that become greater than K as counter reaches K and matches against the condition. This is one-liner and preferred method to achieve this task.
# Python3 code to demonstrate # to remove first K elements matching condition # using itertools.filterfalse() + itertools.count() from itertools import filterfalse, count # initializing list test_list = [3, 5, 1, 6, 7, 9, 8, 5] # printing original list print ("The original list is : " + str(test_list)) # using itertools.filterfalse() + itertools.count() # to remove first K elements matching condition # removes first 4 odd occurrences res = filterfalse(lambda i, counter = count(): i % 2 != 0 and next(counter) < 4, test_list) # printing result print ("The filtered list is : " + str(list(res))) |
The original list is : [3, 5, 1, 6, 7, 9, 8, 5] The filtered list is : [6, 9, 8, 5]
Recommended Posts:
- Python - Sum elements matching condition
- Python | Count of elements matching particular condition
- Python | Summation of first N matching condition
- Python | Remove matching tuples
- Python | Matching elements count
- Python | Count of Matching i, j index elements
- Python | Check if all elements in list follow a condition
- Python - Remove the row if all elements equal to N
- Python | Remove last K elements of list
- Python | Remove Front K elements
- Python | Remove Initial K column elements
- Python - Remove space between tuple elements
- Remove multiple elements from a list in Python
- Python - Remove Negative Elements in List
- Python | Remove trailing empty elements from given list
- Python | Remove List elements containing given String character
- Python - Remove particular data type Elements from Tuple
- Python | Remove unordered duplicate elements from a list
- Python | Remove elements of list that are repeated less than k times
- Check if elements of an array can be arranged satisfying the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

