Python Program to count duplicates in a list of tuples


Checking copies in a list of tuples could be a common errand in information investigation and information preparation. Python gives a few approaches to effectively check the events of tuples in a list. In this article, we'll investigate diverse calculations and their executions to check copies in a list of tuples utilizing Python. We'll cover three approaches: employing a lexicon, the Counter class from the collections module, and leveraging the control of Pandas DataFrames. Understanding these approaches will empower you to viably analyze the recurrence dispersion of information and pick up bits of knowledge from your datasets.

Advantages of Python Program to count duplicates in a list of tuples

Effortlessness and coherence − Python is known for its straightforwardness and lucidness, making code less demanding to type in and get it. Checking copies in a list of tuples utilizing Python is simple and can be done with brief code.

Effective information preparation − Python gives different built-in information structures and libraries optimized for productive information preparation. Instruments such as lexicons, the Counter lesson, and Panda's DataFrame can effectively check copies in a list of tuples without affecting execution.

Adaptability − Python's flexibility permits it to handle both little and huge datasets. The approach depicted in this article can productively handle datasets of changing sizes, ensuring code versatility and great execution indeed when taking care of huge sums of information.

Wealthy environment − Python includes a colossal environment of libraries and bundles that expand its functionality.

Approach 1: Using dictionaries

The first approach is to use a dictionary and count occurrences of tuples in a given list. Here are the steps for this approach −

Algorithm

  • Step 1 − Initialize a purge lexicon to store the number of tuples.

  • Step 2 − Emphasize each tuple within the list.

  • Step 3 − Check on the off chance that the tuple as of now exists within the lexicon.

  • Step 4 − In case so, increase this tuple's check by one. B. On the off chance that no, include a tuple with an introductory check of 1 to the word reference.

  • Step 5 − After repeating all tuples, the lexicon will contain the number of each tuple.

Example

def count_duplicates_dict(tuple_list):
   counts = {}
   for tuple_item in tuple_list:
      if tuple_item in counts:
         counts[tuple_item] += 1
      else:
         counts[tuple_item] = 1
   return counts

students = [('Alice', 90), ('Bob', 75), ('Alice', 90), ('Alice', 90), ('Bob', 75)]
duplicate_counts = count_duplicates_dict(students)
print(duplicate_counts)

Output

{('Alice', 90): 3, ('Bob', 75): 2}

Approach 2: Using Counters from the Debt Collection Module

The moment approach employments the Counter class from the Collections module. It gives a helpful way to check things in a list. Here are the steps for this approach −

Algorithm

  • Step 1 − Import the Counter from the Collections module.

  • Step 2 − Pass a list of tuples as input to initialize a Counter question.

  • Step 3 − Initialize the list of tuples named students.

  • Step 4 − Invoke the function and assign its value to the duplicate_counts.

  • Step 5 − Finally, display the result.

Example

from collections import Counter

def count_duplicates_counter(tuple_list):
   counts = Counter(tuple_list)
   return counts

students = [('Bob', 75), ('Bob', 75), ('Alice', 90), ('Alice', 90), ('Alice', 90)]
duplicate_counts = count_duplicates_counter(students)
print(duplicate_counts)

Output

Counter({('Bob', 75): 2, ('Alice', 85): 3})

Approach 3: Using pandas dataframe

A third approach is to utilize the pandas module to handle the list of tuples as a DataFrame and perform gathering operations to number copies. This approach is valuable when managing expansive datasets or when extra information control and examination are required. Here are the steps for this approach −

Algorithm

  • Step 1 − Import the pandas module.

  • Step 2 − Define a function named count_duplicates_pandas().

  • Step 3 − Create a list of tuples named students.

  • Step 4 − Perform gathering operations on a DataFrame based on tuple columns.

  • Step 5 − Display the number of events of each tuple group.

Example

import pandas as pd

def count_duplicates_pandas(tuple_list):
   df = pd.DataFrame(tuple_list)
   counts = df.groupby(list(df.columns)).size().to_frame('count').reset_index()
   return counts

students = [('Alice', 85), ('Bob', 75), ('Alice', 85), ('Bob', 75), ('Bob', 75)]
duplicate_counts = count_duplicates_pandas(students)
print(duplicate_counts)

Output

       0   1  count
0  Alice  85      2
1    Bob  75      3

Conclusion

In this article, we investigated three distinctive approaches to checking copies in a list of tuples utilizing Python. The user will learn how to utilize lexicons, the Counter class from the Collections module, and the Pandas module to effectively number copies and analyze the recurrence conveyance of your information.

Updated on: 29-Aug-2023

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements