Python – count duplicates in list

 

By using python collections.Counter(<LIST>) we can count number of duplicates in list.

Observer following example for more details:

>>> import collections
>>> mylist = ['a', 'b', 'a', 'c', 'a', 'b']
>>> uniqCount = collections.Counter(mylist)
>>> uniqCount
Counter({'a': 3, 'b': 2, 'c': 1})
>>> uniqCount.keys()
['a', 'c', 'b']
>>> uniqCount.values()
[3, 1, 2]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s