Find Mode of a List in Python

Find Mode of a List in Python

  1. Use the max() Function and a Key to Find the Mode of a List in Python
  2. Use the Counter Class in the Collections Package to Find the Mode of a List in Python
  3. Use the mode() Function From the statistics Module to Find the Mode of a List in Python
  4. Use the multimode() Function From the Statistics Module to Find a List of Modes in Python

A list is one of the most powerful data structures used in Python to preserve the sequence of data and iterate over it. It can contain different data types like numbers, strings, and more.

In a given data set, a mode is a value or element that appears with the highest frequency. There can be one mode, more than one mode, or no mode at all. There will be no mode if all the elements are unique.

In this tutorial, we will discuss how to find the mode of a list in Python.

Use the max() Function and a Key to Find the Mode of a List in Python

The max() function can return the maximum value of the given data set. The key argument with the count() method compares and returns the number of times each element is present in the data set. Therefore, the function max(set(list_name), key = list_name.count) will return the element that occurs the maximum times in the given list that is the required mode of the list.

For Example,

A = [10, 30, 50, 10, 50, 80, 50]
print("Mode of List A is % s" % (max(set(A), key = A.count)))
B = ['Hi', 10, 50, 'Hi', 100, 10, 'Hi']
print("Mode of List B is % s" % (max(set(B), key = B.count)))

Output:

Mode of List A is 50
Mode of List B is Hi

This function will return the smallest mode when there are multiple modes present in the data set.

For Example,

C = [10, 30, 'Hello', 30, 10, 'Hello', 30, 10]
print("Mode of List C is % s" % (max(set(C), key = C.count)))

Output:

Mode of List C is 10

Use the Counter Class in the Collections Package to Find the Mode of a List in Python

The Counter class in the collections package is used to count the number of occurrences of each element present in the given data set.

The .most_common() method of the Counter class returns a list containing two-items tuples with each unique element and its frequency.

For Example,

from collections import Counter
A = [10, 10, 30, 10, 50, 30, 60]
Elements_with_frequency = Counter(A)
print(Elements_with_frequency.most_common())

Output:

[(10, 3), (30, 2), (50, 1), (60, 1)]

The Counter(list_name).most_common(1)[0][0] function will return the required mode of the list. When there are multiple modes present in the list, it will return the smallest mode.

Example :

from collections import Counter
A = [10, 10, 30, 10, 50, 30, 60]
print("Mode of List A is % s" % (Counter(A).most_common(1)[0][0]))

Output:

Mode of List A is 10

Use the mode() Function From the statistics Module to Find the Mode of a List in Python

The mode() function in the python statistics module takes some dataset as a parameter and returns its mode value.

Example :

from statistics import mode
A = [10, 20, 20, 30, 30 ,30]
print("Mode of List A is % s" % (mode(A)))
B = ['Yes', 'Yes', 'Yes', 'No', 'No']
print("Mode of List B is % s" % (mode(B)))

Output:

Mode of List A is 30
Mode of List B is Yes

This function will raise the StatisticsError when the data set is empty or when more than one mode is present. However, in the newer versions of Python, the smallest element will be considered the mode when there are multiple modes of a sequence.

Use the multimode() Function From the Statistics Module to Find a List of Modes in Python

The multimode() function in the statistics module takes some data set as a parameter and returns a list of modes. We can use this function when more than one modal value is present in a given data set.

Example :

from statistics import multimode
A = [10, 20, 20, 30, 30 ,30, 20]
print("Mode of List A is % s" % (multimode(A)))
B = ['Yes', 'Yes', 'Yes', 'No', 'No', 'No', 'Maybe', 'Maybe']
print("Mode of List B is % s" % (multimode(B)))

Output:

Mode of List A is [20, 30]
Mode of List B is ['Yes', 'No']

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python