Count Equal Pairs in List in Python (2 Examples)

 

In this Python article, you will learn how to count the pairs in a list.

The tutorial contains the following content:

Let’s dive right in!

 

Example Data

The first step in this tutorial will be creating an example list:

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"]

Have a look at the previous output! It prints my_list, which contains ten strings. In my_list, “car”, “bus”, “bicycle” and “metro” strings are repeated. Let’s see how to count all these duplicated elements!

 

Example 1: Count Pairs in List Using Counter() Constructor

This example illustrates how to count the repeated items in a list using the Counter class of the collections module in addition to the use of sum() function. To do so, first, we need to import the collections module.

from collections import Counter

Now, we will plug my_list into the Counter() constructor and obtain the total number of pairs in our list using the values() method.

sum(i // 2 for i in Counter(my_list).values())
# 4

As shown, there are four repeated elements of strings in my_list.

 

Example 2: Count Pairs in List Using set() Function & count() Method

It is also possible to count the pairs in a list using the built-in set() function and the count() method together. In addition to them, the // operator, which refer to floor division, is made use of as well.

elements = set(my_list)
results = 0
for i in elements:
   results += my_list.count(i) // 2
 
print(results)
# 4

As shown, first, the set() function converts our list into a set. Then, we iterate through the elements on our list by counting and summing the largest integers obtained after dividing the counts by two. As a result, we get the number of four pairs, just as expected!

 

Video, Further Resources & Summary

Do you need more explanations on how to count the number of pairs in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to count the number of pairs in a list in Python. You can write in the comments section below if you have any additional questions.

 

Paula Villasante Soriano Statistician & R Programmer

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top