Python - Occurrence counter in List of Records


In this article we will explain how to count the occurrences or repetition of elements in the given list of records using Python. Sometimes we need to make a count for the repeated number of items in the given dataset so this article will be helpful to solve these kinds of problems.

Understanding the Problem

The problem we have is to count the repeated items in the given list of records using the Python programming language. So basically we have to show the result of counts of the same or identical items in the given list of records. Let us understand this problem with an example:

Logic for The Above Problem

To solve this problem we use two techniques. First approach will be using the user defined function and the second approach will be using the counter method of the collections module in Python. To understand the full logic for each technique we will dive into the algorithm.

First Approach - Algorithm

Counting the occurrences in the given list of records will be done using the loop through each record and inside the loop we will check the required value or item. Then we will maintain the dictionary to save the counts of different items or values coming during the iteration. If we found the required value then we will update the count in the created dictionary.

  • Step 1 − Define the function called occurrence_counter and pass the data and key inside the function as inputs.

  • Step 2 − Then initiate a counter dictionary and this dictionary will be empty initially to store the result of the function.

  • Step 3 − Now a loop is initiated over the values of the data dictionary. And inside the loop we will get the value of each key using the record.get() method.

  • Step 4 − If the value is not already present inside the counter dictionary then we will add the count of 1. Otherwise increment the value by 1.

  • Step 5 − Repeat the above steps for all the items in the data record.

Example

# Define the function to counter the repeated items
def occurrence_counter(data, key):
   counter = {}
   for record in data:
      value = record.get(key)
      if value is not None:
         counter[value] = counter.get(value, 0) + 1
   return counter

#Initialize the list of records
data = [
   {'color_name': 'Cyan', 'code': '#00FFFF'},
   {'color_name': 'Red', 'code': '#FF0000'},
   {'color_name': 'Cyan', 'code': '#00FFFF'},
   {'color_name': 'Red', 'code': '#FF0000'},
   {'color_name': 'Blue', 'code': '#0000FF'},
]

# Define the key to count
key = 'color_name'
occurrences = occurrence_counter(data, key)
print("Occurrences counter for the given data: \n",occurrences)

Output

Occurrences counter for the given data:
 {'Cyan': 2, 'Red': 2, 'Blue': 1}

Second approach - Algorithm

In this approach we will use the counter class of collections module. With the help of this class we will create a counter object and then we will update the required value from the given list of records.

  • Step 1 − First step is to import the Counter class from the collections module of Python.

  • Step 2 − Then define the function as occurrence_counter and pass two parameters as data and key in the function.

  • Step 3 − Then we will initiate the empty counts object to store the desired result.

  • Step 4 − A loop will be initiated in this step to traverse the attributes of the data one by one.

  • Step 5 − Then we will extract the required value from the data record. And update the counts object accordingly.

Example

# Import the Counter class
from collections import Counter
# Define the function to count the occurrences
def occurrence_counter(data, key):
   counts = Counter()
   for record in data:
      value = record.get(key)
      if value is not None:
         counts[value] += 1
   return counts

#Initialize the list of data
data = [
   {'color_name': 'Cyan', 'code': '#00FFFF'},
   {'color_name': 'Blue', 'code': '#0000FF'},
   {'color_name': 'Cyan', 'code': '#00FFFF'},
   {'color_name': 'Red', 'code': '#FF0000'},
   {'color_name': 'Blue', 'code': '#0000FF'},
]

# Define the key for which we have to make count
key = 'color_name'

# Call the function
occurrences = occurrence_counter(data, key)
print(occurrences)

Output

Counter({'Cyan': 2, 'Blue': 2, 'Red': 1})

Complexity

The time required to complete the execution of both the above codes is O(n), here n is the size of the given input list of records. As we have iterated the given record one time using the loop in both the codes to look up the required key. The space complexity for both the techniques is O(m), here m is the size of the unique elements in the record. As we are storing only unique elements to get the counts.

Conclusion

In this article we have successfully found the occurrence of repeated attributes in the given list of records. We have used two techniques in this article and understood the logic behind each approach. Both the techniques are efficient and effective to find the occurrence counter.

Updated on: 18-Oct-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements