finding items that occur more than once in a list

Paul Rubin http
Tue Mar 18 17:52:25 EDT 2008


sturlamolden <sturlamolden at yahoo.no> writes:
> def nonunique(lst):
>    slst = sorted(lst)
>    return list(set([s[0] for s in
>        filter(lambda t : t[0] != t[1], zip(slst[:-1],slst[1:]))]))

The items are all comparable and you're willing to take them out of order?

from collections import defaultdict
def nonunique(lst):
   d = defaultdict(int)
   for x in lst: d[x] += 1
   return [x for x,n in d.iterkeys() if n > 1]



More information about the Python-list mailing list