43

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.

ruohola
  • 21,987
  • 6
  • 62
  • 97
jimy
  • 593
  • 2
  • 6
  • 9

12 Answers12

45

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.

Anon.
  • 58,739
  • 8
  • 81
  • 86
44

What about

least_common = collections.Counter(array).most_common()[-1]
Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
27

Borrowing the source of collections.Counter.most_common and inverting as appropriate:

from operator import itemgetter
import heapq
import collections
def least_common_values(array, to_find=None):
    counter = collections.Counter(array)
    if to_find is None:
        return sorted(counter.items(), key=itemgetter(1), reverse=False)
    return heapq.nsmallest(to_find, counter.items(), key=itemgetter(1))

>>> data = [1,1,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4]
>>> least_common_values(data, 2)
[(1, 2), (2, 4)]
>>> least_common_values([1,1,2,3,3])
[(2, 1), (1, 2), (3, 2)]
>>>
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
John Machin
  • 81,303
  • 11
  • 141
  • 189
8

Sorry, late to this thread... Found the docs quite helpful: https://docs.python.org/3.7/library/collections.html

Do a search for 'least', and you'll come across this table which helps on getting more than the last (-1) element in the list:

c.most_common()[:-n-1:-1]       # n least common elements

Here's an example:

n = 50

word_freq = Count(words)
least_common = word_freq.most_common()[:-n-1:-1]
June
  • 720
  • 10
  • 22
5
def least_common_values(array, to_find):
    """
    >>> least_common_values([1,1,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4], 2)
    [(1, 2), (2, 4)]
    """
    counts = collections.Counter(array)
    return list(reversed(counts.most_common()[-to_find:]))
Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
5

To just get the least common element and nothing more:

>>> from collections import Counter
>>> ls = [1, 2, 3, 3, 2, 5, 1, 6, 6]
>>> Counter(ls).most_common()[-1][0]
5
ruohola
  • 21,987
  • 6
  • 62
  • 97
3

I guess you need this:

least_common = collections.Counter(array).most_common()[:-to_find-1:-1]
3

The easiest way to implement the search of the minimum in an Iterable is as follows:

Counter(your_iterable).most_common()[-1]

That returns a 2-dimensional tuple containing the element at first position and the count of occurrences at second position.

garciparedes
  • 1,749
  • 2
  • 18
  • 34
bmc
  • 817
  • 1
  • 12
  • 23
1

I would suggest as follows,

least_common = collections.Counter(array).most_common()[len(to_find)-10:len(to_find)]
Saram Han
  • 11
  • 1
0

You can use a key function:

>>> data=[1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4]
>>> min(data,key=lambda x: data.count(x))
1
>>> max(data,key=lambda x: data.count(x))
4
dawg
  • 98,345
  • 23
  • 131
  • 206
0

Based on this answer for most common elements: https://stackoverflow.com/a/1518632

Here is a one liner for obtaining the least common element in a list:

def least_common(lst):
    return min(set(lst), key=lst.count)
khelwood
  • 55,782
  • 14
  • 81
  • 108
Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66
0

To find n least common elements, use the code:

collections.Counter(iterable)[-n:]

For the least common element, n = 1.

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42