Count the duplicates in a Python List

Here’s a nice little function I’ve written to report the number of duplicates in a python list.

from sets import Set
#
def countDuplicatesInList(dupedList):
   uniqueSet = Set(item for item in dupedList)
   return [(item, dupedList.count(item)) for item in uniqueSet]
#
lst = ['I1','I2','I1','I3','I4','I4','I7','I7','I7','I7','I7']
print countDuplicatesInList(lst)

The Set datatype is an unordered set that doesn’t allow duplicates, so the first line in the function adds each item in the original list to the Set. The set automatically throws out duplicates so we end up with a unique list.
The next line creates a tuple of the unique item name and its count in the original list.

The output of the function will look like this:

[('I1', 2), ('I3', 1), ('I2', 1), ('I4', 2), ('I7', 5)]

3 comments

  1. Another way to do this is:

    >>> from collections import defaultdict
    >>> lst = ['I1','I2','I1','I3','I4','I4','I7','I7','I7','I7','I7']
    >>> def count_dups(l):
    … tally = defaultdict(int)
    … for x in l:
    … tally[x] += 1
    … return tally.items()

    >>> count_dups(lst)
    [('I1', 2), ('I3', 1), ('I2', 1), ('I4', 2), ('I7', 5)]

  2. Thanks, this was what I was looking for, couldn’t see how to do it!

  3. Thanks, this was a useful snippet!

Leave a comment