 # Python Program to Delete All Odd Frequency Elements from an Array/List

In the previous article, we have discussed Python Program to Remove Even Frequency Characters from the String

Given a list and the task is to delete all the elements from a given list that has an odd frequency.

Examples:

Example1:

Input:

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

Output:

The list after deletion of all the elements from a given list that has an odd frequency : [10, 4, 2, 10, 2, 4, 6, 6]

Example2:

Input:

Given List = [1, 6, 8, 2, 1, 7, 6, 8]

Output:

The list after deletion of all the elements from a given list that has an odd frequency : [1, 6, 8, 1, 6, 8]

## Program to Delete All Odd Frequency Elements from an Array/List in Python

Below are the ways to delete all the elements from a given list that has an odd frequency:

### 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.
• 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 value even 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 has an odd frequency.
• 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 = [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 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
# 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 even using the if conditional
# statement.
if(freqncyDictionary[lstelmt] % 2 == 0):
# 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 has an odd
# frequency.
print("The list after deletion of all the elements from a given list that has an odd frequency :", modifdlst)


Output:

The list after deletion of all the elements from a given list that has an odd frequency : [10, 4, 2, 10, 2, 4, 6, 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.
• 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 value even 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 has an odd frequency.
• 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
# 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 even using the if conditional
# statement.
if(freqncyDictionary[lstelmt] % 2 == 0):
# 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 has an odd
# frequency.
print("The list after deletion of all the elements from a given list that has an odd frequency :", modifdlst)



Output:

Enter some random list element separated by spaces = 1 6 8 2 1 7 6 8
The list after deletion of all the elements from a given list that has an odd frequency : [1, 6, 8, 1, 6, 8]

### 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)
• 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 value even 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 has an odd frequency.
• 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 = [10, 4, 2, 10, 1, 2, 3, 4, 5, 6, 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)
# 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 even using the if conditional
# statement.
if(freqncyDictionary[lstelmt] % 2 == 0):
# 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 has an odd
# frequency.
print("The list after deletion of all the elements from a given list that has an odd frequency :", modifdlst)


Output:

The list after deletion of all the elements from a given list that has an odd frequency : [10, 4, 2, 10, 2, 4, 6, 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)
• 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 value even 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 has an odd frequency.
• 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)
# 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 even using the if conditional
# statement.
if(freqncyDictionary[lstelmt] % 2 == 0):
# 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 has an odd
# frequency.
print("The list after deletion of all the elements from a given list that has an odd frequency :", modifdlst)


Output:

Enter some random list element separated by spaces = 3 6 2 1 8 3 6 8
The list after deletion of all the elements from a given list that has an odd frequency : [3, 6, 8, 3, 6, 8]


If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.