Velocity Reviews > Re: Find duplicates in a list and count them ...

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

MRAB
Guest
Posts: n/a

 03-26-2009
Benjamin Kaplan wrote:
>
>
> On Thu, Mar 26, 2009 at 5:14 PM, <(E-Mail Removed)
> <mailto(E-Mail Removed)>> 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 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.
>
>
>
> Not all of the values are 1. The 11 duplicates will be higher. Just
> iterate through the dict to find all keys with values > 1.
>
> >>> icounts

> {1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 5, 8: 3, 9: 1, 10: 1, 11: 1}
>
> Python 2.x :
> >>> dups = {}
> >>> for key, value in icounts.iteritems() :

> ... if value > 1 :
> ... dups[key] = value
> ...
> >>> dups

> {8: 3, 1: 2, 7: 5}
>
>
> Python 3.0 :
> >>> dups = {key:value for key, value in icounts.items() if value > 1}
> >>> dups

> {8: 3, 1: 2, 7: 5}
>

The equivalent in Python 2.x would be:

>>> dups = dict((key, value) for key, value in icounts.iteritems() if

value > 1)
>>> dups

{8: 3, 1: 2, 7: 5}