Python | Extract least frequency element
Last Updated :
13 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to extract element which is occurring least number of times in the list. Let's discuss certain ways in which this problem can be solved.
Method #1: Using defaultdict() + loop The combination of above functions can be used to perform this task. In this, we extract each element's frequency using defaultdict() and extract the minimum frequency element after traversing the defaultdict.
Python3
# Python3 code to demonstrate working of
# Extract least frequency element
# using defaultdict() + loop
from collections import defaultdict
# initialize list
test_list = [1, 3, 4, 5, 1, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# Extract least frequency element
# using defaultdict() + loop
res = defaultdict(int)
for ele in test_list:
res[ele] += 1
min_occ = 9999
for ele in res:
if min_occ > res[ele]:
min_occ = res[ele]
tar_ele = ele
# printing result
print("The minimum occurring element is : " + str(tar_ele))
OutputThe original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : 4
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using defaultdict() + loop which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #2: Using Counter() This method is similar to above, the difference is that the frequency is stored using Counter() and then we extract element with least frequency.
Python3
# Python3 code to demonstrate working of
# Extract least frequency element
# using Counter()
from collections import Counter
# initialize list
test_list = [1, 3, 4, 5, 1, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# Extract least frequency element
# using Counter
res = Counter(test_list)
tar_ele = res.most_common()[-1][0]
# printing result
print("The minimum occurring element is : " + str(tar_ele))
OutputThe original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : 4
Time complexity: O(n), where n is the length of the numbers list. The counter() function has a time complexity of O(n)
Auxiliary Space: O(n),where n is the length of the numbers list. This is because the counter() function creates an intermediate result for each element of the numbers list, which requires additional memory.
Method #3 : Using set() and list comprehension
In this method, we can use set() to get the unique elements from the given list and then use list comprehension to find the least occurring element.
Python3
# Python3 code to demonstrate working of
# Extract least frequency element
# using set() and list comprehension
# initialize list
test_list = [1, 3, 4, 5, 1, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# Extract least frequency element
# using set() and list comprehension
unique_elements = set(test_list)
min_occ = min([test_list.count(x) for x in unique_elements])
tar_ele = [x for x in unique_elements if test_list.count(x) == min_occ]
# printing result
print("The minimum occurring element is : " + str(tar_ele))
#this code is contributed by edula vinay kumar reddy
OutputThe original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : [4]
Time Complexity: O(n) where n is the length of the list.
Auxiliary Space: O(n) where n is the length of the list.
Method #4: Using a dictionary to store the frequency of each element:
Python3
test_list = [1, 3, 4, 5, 1, 3, 5]
freq_dict = {}
for i in test_list:
if i not in freq_dict:
freq_dict[i] = 1
else:
freq_dict[i] += 1
min_occ = min(freq_dict.values())
tar_ele = [k for k, v in freq_dict.items() if v == min_occ]
# printing original list
print("The original list : " + str(test_list))
print("The minimum occurring element is:", tar_ele)
#This code is contributed by Jyothi pinjala.
OutputThe original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is: [4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using heapq library
Use the heapq library to extract the least frequency element. The idea is to iterate over each element in the list and store its frequency in a dictionary. Then, use the heapq library to find the smallest value in the dictionary, which will be the least frequency element.
Python3
import heapq
# initialize list
test_list = [1, 3, 4, 5, 1, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# create a dictionary to store the frequency of each element
freq_dict = {}
for elem in test_list:
if elem in freq_dict:
freq_dict[elem] += 1
else:
freq_dict[elem] = 1
# use heapq library to find the smallest value in the dictionary
min_occ = heapq.nsmallest(1, freq_dict.values())[0]
# find all elements that occur with the minimum frequency
tar_ele = [k for k, v in freq_dict.items() if v == min_occ]
# printing result
print("The minimum occurring element is : " + str(tar_ele))
OutputThe original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : [4]
Time complexity: O(nlogk), where n is the length of the list and k is the number of distinct elements in the list.
Auxiliary space: O(k), since we need to store the frequency of each element in the dictionary.
Similar Reads
Python - List Frequency of Elements
We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}.Using collections.Countercollections.Counter class provides a dictionary-like structure that
2 min read
Python - Extract elements with Frequency greater than K
Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read
Extract Elements from a Python List
When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read
Python - Fractional Frequency of elements in List
Given a List, get fractional frequency of each element at each position. Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 4]Â Output : ['1/4', '1/3', '2/4', '1/1', '1/1', '2/3', '3/4', '3/3', '4/4']Â Explanation : 4 occurs 1/4th of total occurrences till 1st index, and so on.Input : test_list = [4, 5, 4,
5 min read
Sort List Elements by Frequency - Python
Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
3 min read
Python | Elements with Frequency equal K
This is one of the most essential operation that programmer quite often comes in terms with. Be it development or competitive programming, this utility is quite essential to master as it helps to perform many tasks that involve this task to be its subtask. Lets discuss approach to achieve this opera
3 min read
Python - Step Frequency of elements in List
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python - Extract elements with equal frequency as value
Given a list, extract all the elements having same frequency as its value. Examples: Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4, 4] Output : [1, 3, 4] Explanation : All elements occur equal times as their value. Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4] Output : [1, 3] Explanation :
6 min read
Python - Restrict Elements Frequency in List
Given a List, and elements frequency list, restrict frequency of elements in list from frequency list. Input : test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6], restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 1} Output : [1, 4, 5, 4, 4, 6] Explanation : Limit of 1 is 1, any occurrence more than that is removed.
5 min read
Python | Frequency grouping of list elements
Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this
6 min read