Find duplicates in a list and count them ...

D'Arcy J.M. Cain darcy at druid.net
Thu Mar 26 15:50:04 EDT 2009


On Thu, 26 Mar 2009 12:22:27 -0700
Paul.Scipione at aps.com wrote:
> I'm a newbie to Python.  I have a list which contains integers (about 80,000).  I want to find a quick way to get the numbers that occur in the list more than once, and how many times that number is duplicated in the list.  I've done this right now by looping through the list, getting a number, querying the list to find out how many times the number exists, then writing it to a new list.  On this many records it takes a couple of minutes.  What I am looking for is something in python that can grab this info without looping through a list.

icount = {}
for i in list_of_ints:
    icount[i] = icount.get(i, 0) + 1

Now you have a dictionary of every integer in the list and the count of
times it appears.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list