Python Program to Find the Mode of a List

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

Before going to the program first, let us understand what is Mode.

Mode:

  • The mode is the value that appears most frequently in the list.

Related: Python Program to Find Unique Elements in a List

Program code to find the mode of a list in Python

# Find the Mode of a List in Python
from collections import Counter

def find_mode(lst):
    frequency = Counter(lst)
    mode_data = frequency.most_common(1)
    return mode_data[0][0]

input_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
mode = find_mode(input_list)
print("Mode of the list:", mode)

Explanation

  1. Function Definition: The find_mode function takes a list as input and returns the mode of the list.
  2. Main Program: The program defines a list and finds the mode using the find_mode function. It then prints the result.

Output

Python Program to Find the Mode of a List

  • When you run the above program, it will find the mode of the list and print the result.

Conclusion

  • In this tutorial, we learned a Python program for finding the mode of a list.
  • Understanding this concept is essential for data analysis and enhancing your programming skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *