Change language

Python | Remove list items that are repeated less than k times

| |

Examples :

  Input:  lst = [’a’,’ a’, ’a’,’ b’, ’b’ , ’c’], k = 2  Output:  [’ a’, ’a’,’ a’, ’b’,’ b’]  Input:  lst = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4], k = 3  Output:  [1, 1, 1, 1, 3, 3, 3 ] 

Approach # 1: Python naive

Counter () from collections module creates a dictionary which maps values ​​to counters and stores them in & # 39; countted & # 39 ;. We then use temp_lst to store the items to be deleted. Finally, we go through the given list and add any items that are not in & # 39; temp_lst & # 39; to & # 39; res_lst & # 39; containing the required output.

# Python3 element remover
# a list that is repeated less than k times

from collections import Counter

 

def removeElements (lst, k):

  counted = Counter (lst)

 

temp_lst = []

for el in counted:

if counted [el] & lt; k:

temp_lst.append (el)

 

res_lst = []

for el in lst:

if el not in temp_lst:

res_lst.append (el)

 

return (res_lst)

 
# Driver code

lst = [ ’a’ , ’ a’ , ’a’ , ’ b ’ , ’ b’ , ’c’ ]

k = 2

print (removeElements (lst, k))

Exit:

 [’a’,’ a’, ’a’,’ b’, ’b’] 

Approach # 2: An Effective Approach

An efficient approach to using the Counter method is to create a dictionary mapping value for the counts and then use a list comprehension to filter out counts that exceed the specified value. This approach is efficient in terms of time and space.

# Python3 program to remove elements
# a list that repeats less than k times

from collections import Counter

 

def removeElements (lst, k):

  counted = Counter (lst)

return [el for el in lst if counted [el]" = k]

 
# Driver code

lst = [ ’ a’ , ’a’ , ’a’ , ’ b’ , ’ b’ , ’c’ ]

k = 2

print (removeElements (lst, k))

Exit:

 [’a’,’ a’ , ’a’,’ b’, ’b’] 

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

News


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

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

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