How to Count the Frequency of an Element in an Unordered List?

How To Count The Frequency Of An Element In An Unordered List

A list is a simple data type that contains items in the form of a sequence. There are two types of lists in python, ‘Ordered list’ and ‘unordered list.’ Let’s understand the ‘unordered list’ and process to count the frequency of elements in an unordered list.

Python is a very easy and simple programming language because of its data types, built-in functions, and understandable syntax. Data types like lists, arrays, and tuples are used to solve many complex problems simply.

Lists are always considered an ordered data type in python. Ordered means all items in the list are in sequence. If we add a new element, it’ll take the last position in the list. But, if the elements are unsorted, we can consider the list as an unordered list.  

What is the unordered list?

The unordered list is the list with no sequence. In simple words, we can also refer to it as an unsorted list. The syntax to write this unordered list is simple. The lists are always enclosed in square brackets.

How to initialize an unordered list?

A = [5,2,3,8,5]
print(A)
Unordered List In Python
Example of an Unordered List In Python

In the above example, the list is printed correctly. This way, we can use the list in python to solve the problems. In some problems in python, like sorting and searching, we need to exclude duplicate items. For this, we need to find the frequency of elements in the list.

when do we need to count the frequency of elements in the list?

We’ll take a simple example to understand the importance of the counting frequency of elements in the list. Refer to the first example, where we can manually tell that the frequency of element ‘5’ is  ‘two.’ Now, consider a class of 100 students. The grades of 100 students are in the form of a list and now, we have to count the number of students who got similar grades. This problem is very complicated and time-consuming to solve manually.

We can solve this problem within seconds using the python code. Similarly, many problems related to the database need such a solution to ease the complexity of the overall problem.

How to count the frequency of elements in the unordered list?

There are several methods to solve this problem using python. we’ll see one by one implementation using the examples.

Method 1: Count frequency using dictionary

To count the frequency of each element in the list, first, we’ll create one list with elements and an empty dictionary. This dictionary will help us to maintain the count of every element in the list. store every element from the list in the dictionary. In the normal way, first, we’ll surf through all the elements of the list. Then check the dictionary for repetition of the element. finally, if the element is present in the dictionary, then increase the count otherwise, make it one.

Let’s understand the algorithm with implementation:

list_check = [1, 2, 3, 5, 6, 3, 2, 7, 4, 0] 
count_frequency = {}    
for item in list_check:
    if item in count_frequency:
        count_frequency[item] += 1  
    else: 
        count_frequency[item]  = 1  
print(count_frequency)   

In this example, ‘list_check’ is a list that will be an input for this code, and ‘count_frequency’ is an empty dictionary to maintain the count of elements in the list. Next, ‘for loop’ is used to check each element in the list. If the elements are repeating, then increase the count otherwise it’ll consider as 1. After this, print the dictionary to check the count.

Frequency Of Elements Using Dictionary
Frequency Of An Elements Using Dictionary

Method 2: Count frequency using the collections module

The basic aim of the Modules in python is to provide very easy solutions to complex problems. Let’s compare method 1 and method 2. Here, in method 2, we only need to follow 3 steps to solve the problem without any loops or if-else conditions. First, we need to import the collections module and initialize the list, then use a Counter function from the collections module. finally, print the result in the form of a dictionary. Only 3-4 lines of code are sufficient to solve this problem.

import collections
list_check =[1, 2, 3, 5, 6, 3, 2, 7, 4, 0]        
count_frequency = collections.Counter(list_check) 
print(dict(count_frequency))                      

In this example, ‘list_check’ contains the list of elements and the Imported collections module to use. ‘Counter’ function.

Collections Module For Frequency Count
Collections Module For Frequency Count

Here, the result is the same as the previous one, implemented through a simple dictionary method. We can ease the process with this module in python. After comparing method 1 and method 2 we can finally see that method 2 is very easy and simple.

Method 3: Count frequency using the numpy library

numpy library is also used to solve this problem using a .unique function. The .unique function calculates the frequency of elements from the list.

let’s implement the code!!

import numpy as num
list_check = [1, 2, 3, 5, 6, 3, 2, 7, 4, 0]
num.unique(list_check, return_counts=True)

Here, ‘list_check’ contains the list of an element. numpy library is imported to use the ‘.unique’ function.

Frequency Count Using Numpy
Frequency Count Using Numpy

Here, array 1 is the list of values from the list, and array 2 is the frequency count of the element in the list.

Summary

In this article, we explore the problem of counting the frequency of an element in the list. This article mainly covers the topics like what is list, types of lists, and details of an unordered list. It also covers details of why we need to find out the frequency of the elements from the list. Finally, this article provides three methods to solve this problem using simple techniques, built-in data types, modules, and libraries. Hope you will enjoy this article.

References

Do read the official documentation of collections module and numpy library for more details.