# Find duplicates in a list and count them ...

John Machin sjmachin at lexicon.net
Fri Mar 27 00:06:46 CET 2009

```On Mar 27, 8:14 am, Paul.Scipi... at aps.com wrote:
> Hi D'Arcy J.M. Cain,
>
> Thank you.  I tried this and my list of 76,979 integers got reduced to a dictionary of 76,963 items, each item listing the integer value from the list, a comma, and a 1.

I doubt this very much. Please show:
(a) your implementation of D'Arcy's suggestion
(b) the code you used that lead you to the conclusion that all counts
were 1. See example below.

>  I think what this is doing is finding all integers from my list that are unique (only one instance of it in the list), instead of creating a dictionary with integers that are not unique, with a count of how many times they occur.  My dictionary should contain only 11 items listing 11 integer values and the number of times they appear in my original list.

The only way of getting your desired result is to get a dict of counts
and then to filter out the ones where the count is greater than one.
D'Arcy appears to have presumed that it was not necessary to show the
second stage :-)

[assuming Python 2.6]
>>> list_of_ints = [999, 2, 3, 999, 2, 2, 8, 42, 999, 42, 5]
>>> len(list_of_ints)
11
>>> icount = {}
>>> for i in list_of_ints:
...     icount[i] = icount.get(i, 0) + 1
...
>>> icount
{2: 3, 3: 1, 5: 1, 999: 3, 8: 1, 42: 2}
>>> len(icount)
6
>>> all(count == 1 for count in icount.itervalues())
False
>>> dups = dict((k, v) for k, v in icount.iteritems() if v > 1)
>>> dups
{2: 3, 42: 2, 999: 3}
>>>

HTH,
John

```