Python statistics.mode() method is “used to calculate the mode (central tendency) of the given numeric or nominal data set.”
data: It is required. The data values to be used (can be any sequence, list, or iterator.
Example 1: How to Use Python statistics.mode() Method
import statistics listA = [19, 21, 46, 19, 18, 19] print(statistics.mode(listA))
In the above code, the number 19 is frequently appearing. So that is our mode.
The mode is the statistical term that refers to the most frequently occurring number found in a set of numbers. The mode is detected by collecting and organizing data to count the frequency of each result.
import statistics listUnique = [19, 21, 18, 30, 46] print(statistics.mode(listUnique))
The above list has unique elements inside the list. So mode does not work here. Instead, it will give us an error.
To calculate the tuple’s mode, pass it as a parameter to the mode() function, and it will return the mode of data.
import statistics data = (21, 19, 18, 46, 30, 18, 19, 21, 18) print(statistics.mode(data))