Count Min & Max Occurrences in List in Python (2 Examples)
In this Python tutorial, we will show how to count minimum and maximum occurrences in a list.
The table of content contains the following:
Let’s dive right into the examples.
Define Example Data
First, we will define some example data for this tutorial:
my_list = ["car", "bicycle", "car","metro", "bus", "plane", "bicycle", "car", "bus", "metro"] print(my_list) # ['car', 'bicycle', # 'car', 'metro', # 'bus', 'plane', # 'bicycle', 'car', # 'bus', 'metro']
The previous output in the Python console shows that our example data is a list object named my_List that contains 10 strings and some are repeated in the list. In the following examples, I will show how to count the minimum and maximum occurrences in our list.
Example 1: Count Most & Least Frequent Elements in List Using Counter Class & most_common() Function
The Counter class of the collections module can be handy in this case. First, we will import it.
from collections import Counter
Now, we can apply the Counter() most_common() functions together to our list object my_list to get the count of each word.
counts_list = Counter(my_list).most_common() print(counts_list) # [('car', 3), ('bicycle', 2), ('metro', 2), ('bus', 2), ('plane', 1)]
We have created a new list of strings, counts_list, which orders the items from the least to most frequent by default. Now we can show the most frequent word and its count by specifying the index position.
As the most frequent element is in the first position of the list, we will use index 0 as Python starts counting from 0.
counts_list[0] # ('car', 3)
As shown, the string “car” is the most frequent on our list. Using index -1, we can also access the least frequent string in counts_list.
counts_list[-1] # ('plane', 1)
Based on the output, the least frequent element on our list is the string “plane”, which appears just once.
Example 2: Count Most & Least Frequent Elements in List Using max() & min() Functions
Alternatively, we can use the max() and min() functions to get the most and least frequent words in our list.
We will first define a function to extract the maximum occurrences using the max() function and key = List.count
argument to sort the words from the most to the least frequent.
def max_occurrences(List): return max(set(List),key = List.count)
Now, we will apply the user-defined max_occurrences() function to our list.
print(max_occurrences(my_list)) # car
As you can see, the printed string is “car”, which is the same word obtained in Example 1 as the most frequent.
To obtain the least frequent, we will repeat the same process using the min() function this time.
def min_occurrences(List): return min(set(List),key = List.count) print(min_occurrences(my_list)) # plane
The output above matches the one obtained in the first example. One more time, the string “plane” was printed as the least frequent word in our list.
Video, Further Resources & Summary
Do you need more explanations on how to obtain the maximum and minimum frequencies in a list? Then you should look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
There are other tutorials on Statistics Globe you could be interested in:
- Lists in Python (13 Examples)
- Access List Element by Index in Python
- Count Duplicates in List in Python
- Introduction to Python
- Convert List from Character String to Integer in Python
This post has shown how to count the minimum and maximum occurrences in a list in Python. If you have any further questions, you can write in the comment section below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter