# Count max Occurrences in List using Python : Different Methods

Python is a popular programming language that may be used on a web application’s backend, frontend, or entire stack. In this article, you will know how to count the most or max occurrences in a Python list.

## Methods to Count max Occurrences in List using Python

Although there are other approaches, we’ll concentrate on two:

### Method 1. Using Counter class from the collections module

Python has a built-in library known as collections, which includes the Counter class. Using the Counter class, you may count how many times a certain element occurs in a collection. For clarity, let’s look at the following example:

``````from collections import Counter
sample_list = [10,10,20,30,40,50,60,60,60]
#creating counter object
count= Counter(sample_list)
#printing the count of each element
for i in count:
print(i,count[i])``````

Output

```10 2
20 1
30 1
40 1
50 1
60 3```

As you can see, the Counter class counts the occurrences of each element in the list. The number 6 occurs three times in this scenario, so the total is 3.

Let’s look at how the Counter class may be used to determine the list’s maximum occurrence count. Like previously, we’ll compile a list including:

``````from collections import Counter
sample_list = [10,10,20,30,40,50,60,60,60]
#creating counter object
count= Counter(sample_list)
#maximume number of occurences
max_count = count.most_common()
print(max_count)``````

Output

`60`

The most common() function displays a list of tuples, where each tuple is of the format (element, count). The element with the greatest number of occurrences appears first in the list.

### Method 2. Use the max() function

Another option is to utilize the max() function, which counts each element in a list and returns the element with the highest numerical value. With the help of a key function, we can extract the count from each tuple, allowing us to locate the element with the highest count:

``````from collections import Counter
sample_list = [10,10,20,30,40,50,60,60,60]
#creating counter object
count= Counter(sample_list)
#maximume number of occurences
max_count = max(count.most_common(), key=lambda x: x)
print(max_count)``````

Output

`(60, 3)`

In the output 60 is the number and 3 is the number of occurrence of this number.

These are the methods you can use the Counter class or the max() function to Count max Occurrences in the List using Python. If you have any questions then you can contact us.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

## SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In: 