Python 3 elegant ways to find most/less common element

If you want to find the most or less common element in a python list of elements than you have a simple but elegant solution by using built in functions of the language. With python and Python 3 you have many option of getting the most or less common element. Here I'll present two most popular one the web:

Video tutorial: Python 3 elegant way to find most/less common element in a list

Most/less common using a Counter from collections

Python 2.7 and above offer very elegant solution to find the most/less frequent elements of a list. The solution is easy to read and use. It offers good level of information and customizations. Let say how to find the most common elements by using it:

from collections import Counter
cnt = Counter()
for my_list in [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]:
	cnt[my_list] += 1
print(cnt.most_common(1))
print(cnt)

result:

[(3, 4)]
Counter({3: 4, 5: 3, 7: 2, 14: 1, 12: 1, 6: 1, 11: 1})

as you can see you have information about the most common element and how many times this element is present in the list. You can check first three elements by function: most_common

from collections import Counter
cnt = Counter()
for my_list in [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]:
	cnt[my_list] += 1
print(cnt.most_common(3))

result:

[(3, 4), (5, 3), (7, 2)]

And what about the less common elements of a list. You can achieve it with counter by a simple change like:

from collections import Counter
cnt = Counter()
for my_list in [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]:
	cnt[my_list] -= 1
print(max(cnt.values()))
print(cnt)

print(cnt.most_common(3))

result:

[(14, -1), (12, -1), (6, -1)]

So this will work if you have more than one element which has max count. You can see also all keys and values from a counter by:

print(list(cnt.keys()))
print(list(cnt.values()))

which result in:

[5, 3, 7, 14, 12, 6, 11]
[3, 4, 2, 1, 1, 1, 1]

Most/less common with count function and set

The first example is to find the most common (or one of the most common elements) of a python list:

my_list = [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]
print(max(set(my_list), key=my_list.count))

result:

3

in case of two elements with equal number of most frequent element. Only one of them will be returned. In order to understand how this code work I will try to split it in parts and explain each part separately.

First we get all unique elements of the list by converting the list to a set. This produce a collection with all unique items from the list

set(my_list)

result is:

{3, 5, 6, 7, 11, 12, 14}

Next step is to pass this set to function max in a statement which is an lambda expression and work with count function for each element. The best way to see what happen is by checking documentation of max: python built function max

and especially this:

The key argument specifies a one-argument ordering function like that used for list.sort()

The full documentation of max:

max(arg1, arg2, *args[, key])
Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).

New in version 3.4: The default keyword-only argument.

Now the previous example can be rewritten by:

my_list = [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]
maxel = 0
el = ''
for i in set(my_list):
	if maxel < my_list.count(i):
		print(my_list.count(i))
		maxel = my_list.count(i)
		el = i
print(el, maxel)

result is:

3 4

If you want to see how count function works in this example you can see also:

print(my_list.count(7))

result:

2

So the explanation of the algorithm for beginners could be:

  • find the unique elements
  • apply count for each elements from the previous step
  • from the returned collection find the max

Another examples to get the idea and exercise with this pythonic code. Find the most common element in a list of strings:

my_list = ['a','b','c','d','e','e','a','c','a']
print(max(set(my_list), key=my_list.count))

result:

a

Find the less common element of a list using the same logic:

my_list = [5, 3, 7, 5, 3, 7, 3, 14, 12, 5, 6, 11, 3]
print(min(set(my_list), key=my_list.count))

result is:

6

Find the less common element from list of strings:

my_list = ['a','b','c','d','e','e','a','c','a']
print(min(set(my_list), key=my_list.count))

result:

b

As a final note lets test the performance of both methods: the testing configuration is:

  • python 3.6
  • PyCharm
  • py-spy and cProfile
  • tests
    • 1,000,000 times find the most common in small list
    • once find the most common in huge list 1,000,000

Both ways show similar performance which is really fast and less than 2 - 3 seconds to find the most common element 1,000,000 times.