Python statistics.mode() Method with Examples

statistics.mode() Method in Python:

Statistics.mode() computes the mode (central tendency) of a numeric or nominal data set.

The mode of a set of data values is the most frequently occurring value. It is the most likely value at which the data will be sampled. A mode of a continuous probability distribution is frequently defined as any value x at which its probability density function has a local maximum value, implying that any peak is a mode.

Syntax:

statistics.mode(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float or nominal value that represents the given data’s mode.

Examples:

Example1:

Input:

Given list = ['hello', 'this', 'is', 'btechgeeks', 'hello']

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Explanation:

Here 'hello' occurs most frequently. Hence the mode is 'hello'.

Example2:

Input:

Given list = [3, 4, 2, 5, 6, 3, 3, 1, 3]

Output:

The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3

statistics.mode() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks', 'hello']
# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 4 2 5 6 3 3 1 3
The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3