Get Number of Duplicates in List in Python (Example Code)

In this post you’ll learn how to count the number of duplicate values in a list object in Python.

Creation of Example Data

x = [1, 3, 4, 2, 4, 3, 1, 3, 2, 3, 3]    # Constructing example list
print(x)
# [1, 3, 4, 2, 4, 3, 1, 3, 2, 3, 3]

Example: Counting List Duplicates

from collections import Counter
x_counts = Counter(x)                    # Applying Counter function
print(x_counts)                          # Returning counts in list
# Counter({3: 5, 1: 2, 4: 2, 2: 2})

2 Comments. Leave new

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.
You need to agree with the terms to proceed

Menu
Top