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] |
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 |
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}) |
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
nice data hack trick
Thanks Lavlesh, glad you like it! 🙂
Regards,
Joachim