There are many methods that can be employed to perform the deletion in the list. Be it remove function, pop function and many other functions. But most of the times, we usually don’t deal with the simple deletion, but with certain constraints. This article discusses certain ways in which we can delete only those elements which occurs less than K times.
Method #1 : Using list comprehension + count()
The idea applied here is to construct a new list using list comprehension and insert only those elements which occur more than K times. The count operation is done with the help of count function.
# Python3 code to demonstrate # remove elements less than and equal K # using list comprehension + count() # initializing list test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1] # printing original list print("The original list : " + str(test_list)) # initializing K K = 2 # using list comprehension + count() # remove elements less than K res = [ i for i in test_list if test_list.count(i) > K] # print result print("The list removing elements less than and equal K : " + str(res)) |
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1] The list removing elements less than and equal K : [3, 2, 3, 3, 2, 2, 2]
Method #2 : Using Counter() + list comprehension
This problem can be efficiently solved using the Counter function that precomputes the count of each elements in list so that the decision to keep or reject a particular element takes lesser time.
# Python3 code to demonstrate # remove elements less than and equal K # using Counter() + list comprehension from collections import Counter # initializing list test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1] # printing original list print("The original list : " + str(test_list)) # initializing K K = 2 # using Counter() + list comprehension # remove elements less than K freq = Counter(test_list) res = [ele for ele in test_list if freq[ele] > K] # print result print("The list removing elements less than and equal K : " + str(res)) |
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1] The list removing elements less than and equal K : [3, 2, 3, 3, 2, 2, 2]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python - Keys Frequency with Value atmost K
- Python - Indices of atmost K elements in list
- Python | Delete elements in range
- Python | List frequency of elements
- Python | Sort list elements by frequency
- Python | Find sum of frequency of given elements in the list
- Python | Group list elements based on frequency
- Python | Frequency grouping of list elements
- Python | Elements with Frequency equal K
- Python - Step Frequency of elements in List
- Python - Similar Consecutive elements frequency
- Python - Similar index elements frequency
- Python - Elements frequency count in multiple lists
- Python - Elements Frequency in Mixed Nested Tuple
- Python - Elements frequency in Tuple Matrix
- Python - Frequency of elements from other list
- Python - Extract elements with Frequency greater than K
- Python - Restrict Elements Frequency in List
- Python - Elements frequency in Tuple
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.

