Find Most Common Element in List in Python (2 Examples)

 

Hi! This short tutorial will show you how to get the most frequently occurring element in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example List of Integers

We will create an example Python list of integers that will be used in the examples in this tutorial. So, in your favorite Python IDE, run the code below to create the example list of integers:

my_list = [1, 2, 3, 4, 1, 2, 4, 5, 6, 3, 2]
 
print(my_list)
 
# [1, 2, 3, 4, 1, 2, 4, 5, 6, 3, 2]
 
print(type(my_list))
 
# <class 'list'>

Now that we have created the sample list, we will explore a couple of ways of determining the most common element.
 

Example 1: Get Most Frequently Occurring Element in List Using NumPy

For this example, we will need first to install and import Python’s NumPy library. Therefore, run the lines of code below to do so:

# install NumPy
pip install numpy
 
# import NumPy
import numpy as np

With NumPy installed and imported into our Python environment, let us now see how to use it to get the most common element in the list:

counts = np.bincount(my_list)
 
most_common_element = np.argmax(counts)
 
print(f"The most common element is {most_common_element}")
 
# The most common element is 2

In the example above, we first used the np.bincount() function to count the number of times each element occurs in the list, then we used the np.argmax() function to get the common element.
 

Example 2: Get Most Frequently Occurring Element in List Using collections Module

In this example, we will make use of the Counter() function from Python’s built-in collections module to build our solution:

from collections import Counter
 
counter = Counter(my_list)
 
most_common_element, count = counter.most_common(1)[0]
 
print(f"The most common element is {most_common_element}")
 
# The most common element is 2

In the above example, we, first of all, counted the number of times each element occurs in the list using the Counter() function, which we assigned to the variable counter. The function returns a dictionary of key-value pairs, with the elements being the key and their counts being the value.

Then, from the counter object, we extracted the most common element using the most_common() method, which returns a list of tuples of the form (element, count). We then assigned the element to the most_common_element variable, and count to the count variable in one line.

So, with these two examples, we have demonstrated how to easily find the most common element in a list in Python. Your use case may determine which method to apply.

I hope you found this tutorial beneficial!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the most common element in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the most common element in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the most common element in a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top