To find the most common, I know I can use something like this:
most_common = collections.Counter(list).most_common(to_find)
However, I can't seem to find anything comparable, for finding the least common element.
Could I please get recommendations on how to do.
Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method.
Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.
most_common
without any argument returns all the entries, ordered from most common to least.
So to find the least common, just start looking at it from the other end.
What about
least_common = collections.Counter(array).most_common()[-1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With