# Python Program to Remove Elements from the Array/List Which Occurs More than k Times

In the previous article, we have discussed Python Program to Delete All Odd Frequency Elements from an Array/List

Given a list, k value and the task is to delete all the elements from a given list that appears more than k times in python.

Examples:

Example1:

Input:

Given List = [4, 5, 8, 9, 4, 6, 3, 3, 3]
Given k value = 2

Output:

The list [4, 5, 8, 9, 4, 6, 3, 3, 3] after deletion all the elements from a given list that appears more than k times:
[4, 5, 8, 9, 4, 6]

Example2:

Input:

Given List = [5, 6, 8, 1, 5, 6, 1, 1, 1]
Given k value = 3

Output:

The list [5, 6, 8, 1, 5, 6, 1, 1, 1] after deletion all the elements from a given list that appears more than k times:
[5, 6, 8, 5, 6]

## Program to Remove Elements from the Array/List Which Occurs More than k Times in Python

Below are the ways to delete all the elements from a given list that appear more than k times in python:

### Method #1: Using Dictionary (Hashing, Static Input)

Approach:

• Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
• Give the list as static input and store it in a variable.
• Loop in the given list using the For loop.
• Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘in‘ keyword.
• If it is true then increment the count of the list element in the dictionary by 1.
• Else initialize the dictionary with the list element as key and value as 1.
• Give the k value as static input and store it in a variable.
• Take a new empty list say modifdlst and store it in a variable.
• Loop in the given list using the For loop.
• Check if the key in the freqncyDictionary having a value less than or equal to k using the if conditional statement.
• If it is true then append the key value to the above declared empty list modifdlst.
• Print the list after deletion of all the elements from a given list that appears more than k times
• The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the list as static input and store it in a variable.
gvnlst = [4, 5, 8, 9, 4, 6, 3, 3, 3]
# Loop in the given list using the For loop.
for i in gvnlst:
# Inside the For loop,
# Check if the list element is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the list element
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the list element as key and value as 1.
else:
freqncyDictionary[i] = 1
# Give the k value as static input and store it in a variable.
k = 2
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the given list using the For loop.
for lstelmt in gvnlst:
# Check if the key in the freqncyDictionary having value less than or equal to k
# using the if conditional statement.

if(freqncyDictionary[lstelmt] <= k):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(lstelmt)
# Print the list after deletion of all the elements from a given list that appears
# more than k times
print("The list", gvnlst,
"after deletion all the elements from a given list that appears more than k times:")
print(modifdlst)


Output:

The list [4, 5, 8, 9, 4, 6, 3, 3, 3] after deletion all the elements from a given list that appears more than k times:
[4, 5, 8, 9, 4, 6]

### Method #2: Using Dictionary (Hashing, User Input)

Approach:

• Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
• Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
• Loop in the given list using the For loop.
• Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘in‘ keyword.
• If it is true then increment the count of the list element in the dictionary by 1.
• Else initialize the dictionary with the list element as key and value as 1.
• Give the k value as user input using the int(input()) function and store it in a variable.
• Take a new empty list say modifdlst and store it in a variable.
• Loop in the given list using the For loop.
• Check if the key in the freqncyDictionary having a value less than or equal to k using the if conditional statement.
• If it is true then append the key value to the above declared empty list modifdlst.
• Print the list after deletion of all the elements from a given list that appears more than k times
• The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the list as user input using the list(),map(),split(),int functions
# and store it in a variable.
gvnlst = list(
map(int, input('Enter some random list element separated by spaces = ').split()))
# Loop in the given list using the For loop.
for i in gvnlst:
# Inside the For loop,
# Check if the list element is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the list element
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the list element as key and value as 1.
else:
freqncyDictionary[i] = 1
# Give the k value as user input using the int(input()) function and store it in a variable.
k = int(input("Enter some random number ="))
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the given list using the For loop.
for lstelmt in gvnlst:
# Check if the key in the freqncyDictionary having value less than or equal to k
# using the if conditional statement.

if(freqncyDictionary[lstelmt] <= k):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(lstelmt)
# Print the list after deletion of all the elements from a given list that appears
# more than k times
print("The list", gvnlst,
"after deletion all the elements from a given list that appears more than k times:")
print(modifdlst)


Output:

Enter some random list element separated by spaces = 3 1 5 6 1 2 2
Enter some random number =1
The list [3, 1, 5, 6, 1, 2, 2] after deletion all the elements from a given list that appears more than k times:
[3, 5, 6]

### Method #3: Using Counter() function (Hashing, Static Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as static input and store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyDictionary)
• Give the k value as static input and store it in a variable.
• Take a new empty list say modifdlst and store it in a variable.
• Loop in the given list using the For loop.
• Check if the key in the freqncyDictionary having a value less than or equal to k using the if conditional statement.
• If it is true then append the key value to the above declared empty list modifdlst.
• Print the list after deletion of all the elements from a given list that appears more than k times
• The Exit of the Program

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as static input and store it in a variable.
gvnlst = [5, 6, 8, 1, 5, 6, 1, 1, 1]
# Calculate the frequency of all the given list elements using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnlst)
# Give the k value as static input and store it in a variable.
k = 3
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the given list using the For loop.
for lstelmt in gvnlst:
# Check if the key in the freqncyDictionary having value less than or equal to k
# using the if conditional statement.

if(freqncyDictionary[lstelmt] <= k):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(lstelmt)
# Print the list after deletion of all the elements from a given list that appears
# more than k times
print("The list", gvnlst,
"after deletion all the elements from a given list that appears more than k times:")
print(modifdlst)


Output:

The list [5, 6, 8, 1, 5, 6, 1, 1, 1] after deletion all the elements from a given list that appears more than k times:
[5, 6, 8, 5, 6]

### Method #4: Using Counter() function (Hashing, User Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyDictionary)
• Give the k value as user input using the int(input()) function and store it in a variable.
• Take a new empty list say modifdlst and store it in a variable.
• Loop in the given list using the For loop.
• Check if the key in the freqncyDictionary having a value less than or equal to k using the if conditional statement.
• If it is true then append the key value to the above declared empty list modifdlst.
• Print the list after deletion of all the elements from a given list that appears more than k times
• The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as user input using the list(),map(),split(),int functions
# and store it in a variable.
gvnlst = list(
map(int, input('Enter some random list element separated by spaces = ').split()))
# Calculate the frequency of all the given list elements using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnlst)
# Give the k value as user input using the int(input()) function and store it in a variable.
k = int(input("Enter some random number ="))
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the given list using the For loop.
for lstelmt in gvnlst:
# Check if the key in the freqncyDictionary having value less than or equal to k
# using the if conditional statement.

if(freqncyDictionary[lstelmt] <= k):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(lstelmt)
# Print the list after deletion of all the elements from a given list that appears
# more than k times
print("The list", gvnlst,
"after deletion all the elements from a given list that appears more than k times:")
print(modifdlst)


Output:

Enter some random list element separated by spaces = 3 1 5 6 1 2 2
Enter some random number =1
The list [3, 1, 5, 6, 1, 2, 2] after deletion all the elements from a given list that appears more than k times:
[3, 5, 6]

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.