How To Find the Most Common Elements of the List in Python?

In today’s article, let’s discuss one of the most asked interview questions: How to find the most common elements of the list in Python?

Although this might sound like a simple question, the approach we use to answer this question gives the interviewer an idea of our knowledge and expertise in Python. 

Understanding the Challenge by Examples

Let us first understand this problem with some examples.

Example 1: Consider a list as shown below.

lst=[1,2,3,3,4,4,4,5,5]

The most common element of this list is 4. Because 4 has been repeated 3 times in the list. 

Example 2: Consider the list below.

lst=[1,2,3,3,4.5,4.5,4.5,5]

This list contains elements of different but compatible data types, int, and float.

The most common element is 4.5. It has occurred 3 times.

Example 3: Check the list below.

lst=[1,2,3,3,5,5]

This list contains 3 and 5, two times each. Hence both 3 and 5 are the most common elements.

Example 4: Consider a list as shown below.

lst=[1,2,"one","one","one",1]

This list contains two data types int and strings. The most common element is "one".

In this article, let us discuss different ways of finding the most common elements and then determining the one that is best suited for various cases. 

Method 1: Using Simple “for” Loop

To find the most common element from the list, we can do the following:

  • Create a new list with a tuple (frequency, element) for every unique element in the list.
  • Find the maximum value based on the frequency.
  • Now find all the elements with this maximum frequency.

When we are sure that the list contains only one most common element, we can use the following code snippet.

lst=[1,2,3,3,4,4,4,5,5]
print(max(set(lst),key=lst.count))

Output:

4

When the list contains more than one most common elements, we can use the following code:

lst=[1,2,3,3,5,5]
lst_1=[(lst.count(x),x) for x in set(lst)]

#extract the frequency from the tuple(freq,ele)
max_count=max(lst_1)[0]

#print the element if the frequency is equal to maximum count
for ele in lst_1:
   if ele[0]==max_count:
       print(ele[1])

Output:

3
5

This method also works when there are multiple data types. Refer to the below example.

lst=[1,2,"one","one","one",1]
lst_1=[(lst.count(x),x) for x in set(lst)]
max_count=max(lst_1)[0]
for ele in lst_1:
   if ele[0]==max_count:
       print(ele[1])

Output:

one

Method 2: Using mode(), multimode()

In statistical terms, the mode of the list returns the most common elements from it. We can use this to determine the most common elements from a list.

Python has a standard module named statistics which contains two functions named mode and multimode

  • mode() – returns the most common element from the list. However, when there is more than one element, it would return the first element in the order of occurrence.
  • multimode() – return the most common elements from the list. This returns all the most common elements from the list.

Now, let’s learn how to use these functions. Firstly, we have to import mode() and multimode() functions from the statistics module in our program using the below line.

from statistics import multimode, mode

Now, let’s try to find the most common elements from the list [1,2,3,3,4,4,4,5,5]

from statistics import multimode, mode

lst=[1,2,3,3,4,4,4,5,5]
print(mode(lst))

Output:

4

Now, let’s try to use mode() on the list that has more than one most common element.

from statistics import mode

lst=[1,2,3,3,5,5]
print(mode(lst))

Output:

3

As we can see from the output, although 3 and 5 are the two most common elements, only 3 is considered.

In cases like this, we can use the multimode() function as shown below.

from statistics import multimode
lst=[1,2,3,3,5,5]
print(multimode(lst))

Output:

[3, 5]

Note that, this method works when we have different data types in the list.

from statistics import multimode,mode

lst=[1,2,"one","one","one",1]
print(multimode(lst))

Output:

['one']

Method 3: Using Counter

There is a module named collections in Python which has a class called Counter.

The Counter class has a method named most_common(). This function returns a list of tuples, where the first element within the tuple represents the element in the list and the second element represents the occurrence frequency.

πŸ’‘ Note: The elements in the list are sorted based on the frequency.

To use this, we have to first import the Counter class from the collections module as shown below.

from collections import Counter

Now, let’s try finding the most common elements.

from collections import Counter

lst=[1,2,3,3,4,4,4,5,5]
#create an instance of the Counter Object
c=Counter(lst)
#find the common elements
print(c.most_common())

Output:

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

If you are sure that the list would contain only one most common element, then you can access the element using list slicing as shown below

from collections import Counter

lst=[1,2,3,3,4,4,4,5,5]
c=Counter(lst)
print(c.most_common(1)[0][0])

Output:

4

If we expect more than one most common element in the list, then we can do the following:

from collections import Counter

lst=[1,2,3,3,4,4,4,5,5,5]
c=Counter(lst)
max_occurence=c.most_common(1)[0][1]
for ele in c.most_common():
   if ele[1] == max_occurence:
       print(ele[0])

Output:

4
5

Now, let’s look at an example where the list contains different data types.

from collections import Counter

lst=[1,2,"one","one","one",1]
c=Counter(lst)
print(c.most_common(1)[0][0])

Output:

one

As seen from the above output, this method works on a list with different data types.

Conclusion

In this article, we have discussed various ways of finding the most common elements of the list.

We hope this post has been informative. For more such content, subscribe to our weekly email newsletter here:

Thank you for reading.