finding items that occur more than once in a list
Raymond Hettinger
python at rcn.com
Tue Mar 18 13:17:19 EDT 2008
On Mar 18, 2:57 am, Simon Forman <sajmik... at gmail.com> wrote:
> Is there a more efficient way to do this?
>
> def f(L):
> '''Return a set of the items that occur more than once in L.'''
> L = list(L)
> for item in set(L):
> L.remove(item)
> return set(L)
>
> |>> f([0, 0, 1, 1, 2, 2, 3])
> set([0, 1, 2])
def f(L):
seen = set()
dups = set()
for e in L:
if e in seen:
dups.add(e)
else:
seen.add(e)
return dups
Raymond
More information about the Python-list
mailing list