To find the element with the minimum number of occurrences in a list in Python, you can use the collections.Counter class to count the occurrences.
In this example,
Counter
class from the collections
module.data
with several integer elements.Counter
to count the occurrences of each element.min
function on the values of the counter.from collections import Counter
data = [1, 2, 2, 3, 3, 3, 4]
count = Counter(data)
least_common = min(count, key=count.get)
print(f'Element with the minimum occurrences is: {least_common}')
Element with the minimum occurrences is: 1
In this example,
Counter
class from the collections
module.words
with several string elements.Counter
to count the occurrences of each element.min
function on the values of the counter.from collections import Counter
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
count = Counter(words)
least_common = min(count, key=count.get)
print(f'Element with the minimum occurrences is: {least_common}')
Element with the minimum occurrences is: orange
In this tutorial, we learned How to find the Element with Minimum Number of Occurrences in a List in Python language with well detailed examples.