Python - Multimode of List


In the given problem we are required to show the element which is occurring most frequently in the given list using Python. Basically this operation is known as multimode of list in Python.

Understanding the logic for the Problem

The problem at hand is to create a program which will perform the operation to find the multimode of the given list. So the term multimode is used in the list to refer to the set of items which occur most frequently in the input list. Or we can say that the highest frequency or count for an item of the list.

For finding the multimode of a given list we will determine the frequency count for every item in the given list. And show the item which is having the highest count or frequency. To solve this problem we will use two approaches. First we will use the statistics module of Python and in the second approach we will use basic Python functionality.

Algorithm - Using Statistics

  • Step 1 − First we will import the statistics module, in which we will be having the multimode function to return the most frequent item of the list.

  • Step 2 − So define the multimode and inside this function we will pass a parameter of list for which we have to find out the multimode.

  • Step 3 − Define and initialize the list as the_list. And call the function to show the Output.

Example - Using Statistics

import statistics #import the module

# module usage 
def multimode(lst):
   return statistics.multimode(lst)

# usage
the_list = [9, 8, 8, 7, 7, 7, 6, 6, 6, 6]
Output = multimode(the_list)
print(Output)

Output

[6]

Algorithm - Another Approach

  • Step 1 − First we will create a dictionary called most_frequent which is used to store the frequency count for the items in the list.

  • Step 2 − Then we will iterate the numbers of the list.

  • Step 3 − And check for every number. If the number is already present in the most frequent dictionary. If the condition is true then increase the count by 1 using the get method and store count in the dictionary.

  • Step 4 − If the condition is false means the element is not present in the most_frequent dictionary then add it in the most_frequent dictionary with the initial count of 1.

  • Step 5 − As all the items have to be iterated of the list then we will use the max function on the most_frequent dictionary to get the maximum count value. And return the value of max_count.

Example - Another Approach

# Function to find the multimode of the list
def multimode(lst):
   most_frequent = {}
   for num in lst:
      most_frequent[num] = most_frequent.get(num, 0) + 1

   max_count = max(most_frequent.values())
   multimode_values = [num for num, count in most_frequent.items() if count == max_count]
   
   return multimode_values

# usage
my_list = [9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 6, 6]
Output= multimode(my_list)
print(Output)

Output

[8]

Complexity

The time complexity for finding the multimode of the list using Python is O(n) in both the cases, in which n is the size of the given list. As we have iterated over the items in the list once. So the time depends on the size of the given list.

Conclusion

We have successfully created the program for finding the multimode of the given list. We have used two approaches in this article. In the first approach we have used the statistics module and in the second method we have used basic functionality and logic of Python.

Updated on: 17-Oct-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements