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

ALAN GAULD alan.gauld at btinternet.com
Fri Jun 11 19:48:29 CEST 2010



> > counts = 
> > set(( item, mylist.count(item)) for item in mylist if mylist.count(item) > 
> Whee, this is great!  I learned a lot today.  

I should have added that although thats a one liner in code terms it does 
involve iterating over the list twice - in count() - for each element. So it 
might not be very fast for big lists, certainly there are more efficient 
solutions if you don't mind writing more code.

But for most lists you will likely find it is fast enough, certainly for 18 items!

Alan G.



More information about the Tutor mailing list