mode() & multimode() Functions of statistics Module in Python (2 Examples)

 

In this article, I’ll explain how to apply the mode function of the statistics module in Python.

The table of content is structured as follows:

Let’s dig in:

 

Example 1: Get Mode Using mode() Function of statistics Module

In this section, I’ll demonstrate how to get the mode of a list object using the mode() function of the statistics module.

For this task, we first have to create an example list:

my_list1 = [4, 3, 8, 3, 8, 2, 3, 8, 8, 7, 3, 3]    # Create example list
print(my_list1)                                    # Print example list
# [4, 3, 8, 3, 8, 2, 3, 8, 8, 7, 3, 3]

Furthermore, we have to import the statistics module:

import statistics                                  # Import statistics

Now, we can apply the statistics.mode command to return the mode of our list:

print(statistics.mode(my_list1))                   # Apply mode() function
# 3

As you can see, the mode of our list is 3.

 

Example 2: Get Multiple Modes Using multimode() Function of statistics Module

Example 2 illustrates how to return multiple modes using the statistics module in Python.

First, let’s create another list object:

my_list2 = [4, 3, 8, 3, 8, 2, 3, 8, 8, 7, 3, 5]    # Create example list
print(my_list2)                                    # Print example list
# [4, 3, 8, 3, 8, 2, 3, 8, 8, 7, 3, 5]

Note that all values in this list are equal to the list that we have used in Example 1, but the last value is 5 instead of 3.

If we now apply the mode function to this list, the error message “StatisticsError: no unique mode; found 2 equally common values” is returned:

print(statistics.mode(my_list2))                   # Apply mode() function
# StatisticsError: no unique mode; found 2 equally common values

The reason for this is that our new list object contains two different values with the same count (i.e. 3 and 8).

Note that if you are using Python 3.8 or later, the first mode that is found in the list would be returned.

Fortunately, newer versions of the statistics module provide a function called multimode. This function returns a list of all modes in our data:

print(statistics.multimode(my_list2))              # Apply multimode() function
# [3, 8]

In this specific case the values 3 and 8 are returned when applying the multimode function to our list.

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. I demonstrate the Python code of this tutorial in the video.

 

The YouTube video will be added soon.

 

In addition, you might have a look at the related tutorials on this homepage. You can find a selection of articles that are related to the application of the statistics.mode function below.

 

In this tutorial you have learned how to use the mode function of the statistics module in Python programming. Don’t hesitate to let me know in the comments, in case you have any additional questions. In addition, don’t forget to subscribe to my email newsletter to receive regular updates on new posts.

 

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.

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

Menu
Top