[Tutor] Looking for duplicates within a list [SOLVED]

Alan Gauld alan.gauld at btinternet.com
Fri Jun 11 20:19:52 CEST 2010


<davidheiserca at gmail.com> wrote 

> How about this?
> 
> List = [1, 2, 3, 3, 3, 4, 5, 5]
> for Item in list(set(List)):
>     print Item, List.count(Item)

Not bad and you don't need the convert back to list()
But it doesn't filter out those items which are unique 
which the OP asked for.

So I guess it becomes

for item in set(List):
    n = List.count(item)
    if n > 1:
        print item, n

which is still pretty clear.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list