finding items that occur more than once in a list

sturlamolden sturlamolden at yahoo.no
Tue Mar 18 19:08:04 EDT 2008


On 18 Mar, 23:45, Arnaud Delobelle <arno... at googlemail.com> wrote:

> > def nonunique(lst):
> >    slst = sorted(lst)
> >    dups = [s[0] for s in
> >         filter(lambda t : t[0] == t[1], zip(slst[:-1],slst[1:]))]
> >    return [dups[0]] + [s[1] for s in
> >         filter(lambda t : t[0] != t[1], zip(dups[:-1],dups[1:]))]
>
> Argh!  What's wrong with something like:
>
> def duplicates(l):
>     i = j = object()
>     for k in sorted(l):
>         if i != j == k: yield k
>         i, j = j, k


Nice, and more readable. But I'd use Paul Robin's solution. It is O(N)
as opposed to ours which are O(N log N).









More information about the Python-list mailing list