Change language

# Python | Remove elements with a frequency of no more than K

|

Method # 1: Using list comprehension + `count()`
The idea applied here is to create a new list using list comprehension and insert only those elements that occur more than K times. The counting operation is performed using the counting function.

 ` # Python3 demo code ` ` # remove items less and equal to K ` ` # using list comprehension + count () `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 1 ` `] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list )) `   ` # initializing K ` ` K ` ` = ` ` 2 `   ` # using a list comprehension + count () ` ` # remove items 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)) `

Output:

` 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 effectively solved by using the Counter function, which pre-calculates the number of each item in the list, so the solution is it takes less time to save or reject a specific element.

 < code class = "comments"> # Python3 demo code ` # remove items less than and equal to K ` ` # using Counter () + list comprehension ` ` from ` ` collections ` ` import ` ` Counter `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 1 ` `] `   ` # prints the original list ` ` print ` ` (` ` "The original list : "` ` + ` ` str ` ` (test_list)) `   ` # initialization K ` ` K ` ` = ` ` 2 ` ` `  ` # using Counter () + list comprehension ` ` # remove items 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)) `

Output:

` 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] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically