Python Program to Remove Elements from the Array/List Which Appear Strictly Less than k Times

In the previous article, we have discussed Python Program to Remove String Elements that Appear Strictly Less than k Times

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

Examples:

Example1:

Input:

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

Output:

The list [2, 4, 6, 2, 3, 5, 6] after deletion all the elements from a given list that appears strictly less than k times:
[2, 6, 2, 6]

Example2:

Input:

Given List = [10, 20, 30, 40, 20, 10, 20, 10, 30]
Given k value = 2

Output:

The list [10, 20, 30, 40, 20, 10, 20, 10, 30] after deletion all the elements from a given list that appears strictly less than k times:
[10, 20, 20, 10, 20, 10]

Program to Remove Elements from the Array/List Which Appear Strictly Less than k Times in Python

Below are the ways to delete all the elements from a given list that appears Strictly less 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 greater than 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 strictly less 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 = [2, 4, 6, 2, 3, 5, 6]
# 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 = 1
# 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 greater than 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
# Strictly less than k times.
print("The list", gvnlst,
      "after deletion all the elements from a given list that appears strictly less than k times:")
print(modifdlst)

Output:

The list [2, 4, 6, 2, 3, 5, 6] after deletion all the elements from a given list that appears strictly less than k times:
[2, 6, 2, 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 greater than 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 strictly less 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 greater than 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
# Strictly less than k times.
print("The list", gvnlst,
      "after deletion all the elements from a given list that appears strictly less than k times:")
print(modifdlst)

Output:

Enter some random list element separated by spaces = 10 20 30 40 20 10 20 10 30
Enter some random number =2
The list [10, 20, 30, 40, 20, 10, 20, 10, 30] after deletion all the elements from a given list that appears strictly less than k times:
[10, 20, 20, 10, 20, 10]

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 greater than 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 strictly less 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 = [2, 4, 6, 2, 3, 5, 6]
# 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 = 1
# 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 greater than 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
# Strictly less than k times.
print("The list", gvnlst,
      "after deletion all the elements from a given list that appears strictly less than k times:")
print(modifdlst)

Output:

The list [2, 4, 6, 2, 3, 5, 6] after deletion all the elements from a given list that appears strictly less than k times:
[2, 6, 2, 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 greater than 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 strictly less 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 greater than 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
# Strictly less than k times.
print("The list", gvnlst,
      "after deletion all the elements from a given list that appears strictly less than k times:")
print(modifdlst)

Output:

Enter some random list element separated by spaces = 10 20 30 40 20 10 20 10 30
Enter some random number =2
The list [10, 20, 30, 40, 20, 10, 20, 10, 30] after deletion all the elements from a given list that appears strictly less than k times:
[10, 20, 20, 10, 20, 10]

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.