Finding the mode of a list is a common task when you are working with data or statistics in Python.
The mode is the most frequently occurring value or values in a data set. In this tutorial, we will go through how to find the mode of a list in Python, covering multiple techniques to achieve this, including using the built-in Python libraries and writing a custom function.
Step 1: Using the Python statistics module
Python includes a statistics module starting from version 3.4. To use this module to find the mode of a list in Python, first, we’ll have to import the module, then call the statistics.mode() function with our list as the argument.
1 2 3 4 5 6 |
import statistics data = [1, 2, 3, 3, 4, 4, 4, 5, 6, 7] mode_data = statistics.mode(data) print(mode_data) |
If the list contains multiple modes (i.e., more than one value has the highest frequency), the statistics.mode() function will raise a StatisticsError.
1 2 3 4 5 6 |
data_with_multiple_modes = [1, 2, 3, 3, 4, 4] try: mode_data = statistics.mode(data_with_multiple_modes) except statistics.StatisticsError as e: print(e) |
Output
no unique mode; found 2 equally common values
Step 2: Using the collections Counter class
To find multiple modes in a list, we can use the Counter class from the collections module. The Counter class allows us to count the frequency of the elements in a list and find multiple modes if they exist.
Here’s how to use the Counter class to find the mode of a list in Python:
1 2 3 4 5 6 7 8 9 10 11 |
from collections import Counter def find_modes(lst): counter = Counter(lst) max_freq = counter.most_common(1)[0][1] modes = [num for num, freq in counter.items() if freq == max_freq] return modes data = [1, 2, 3, 3, 4, 4, 4, 5, 6, 7] modes = find_modes(data) print(modes) |
This function works even if the list contains multiple modes:
1 2 3 |
data_with_multiple_modes = [1, 2, 3, 3, 4, 4] modes = find_modes(data_with_multiple_modes) print(modes) |
Output
[3, 4]
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import statistics from collections import Counter data = [1, 2, 3, 3, 4, 4, 4, 5, 6, 7] # Using statistics module mode_data = statistics.mode(data) print("Using statistics module:", mode_data) # Using custom function with Counter class def find_modes(lst): counter = Counter(lst) max_freq = counter.most_common(1)[0][1] modes = [num for num, freq in counter.items() if freq == max_freq] return modes modes = find_modes(data) print("Using Counter class:", modes) data_with_multiple_modes = [1, 2, 3, 3, 4, 4] modes = find_modes(data_with_multiple_modes) print("Multiple modes:", modes) |
Output
Using statistics module: 4 Using Counter class: [4] Multiple modes: [3, 4]
Conclusion
In this tutorial, we’ve learned how to find the mode of a list in Python using the built-in statistics module and the Counter class from the collections module. Additionally, we demonstrated how to handle cases where multiple modes exist within the data set. With these tools, you can now analyze the most frequently occurring values in your Python projects!