4

I am working on code snippet to extract duplicates from a list. I have seen several implementations/solutions on this site. However, I am not getting this line correctly - syntax wise I think. After sorting, compare index(x) with index(x+1). If it is add to the set.

print(set([i for i in a if (a[i] == a[i+1]))

a = [1,2,3,2,1,5,6,5,5,5]
print(a)
print(set(sorted(a)))
# l1[i] == l1[i+1]
print(set([i for i in a if (a[i] == a[i+1]))
print(set([i for i in a if sum([1 for item in a if item == i]) > 1]))

Expected results: {1, 2, 5}

RSSregex
  • 179
  • 7
  • 1
    Possible duplicate of [How do I find the duplicates in a list and create another list with them?](https://stackoverflow.com/questions/9835762/how-do-i-find-the-duplicates-in-a-list-and-create-another-list-with-them) – akaur Apr 17 '19 at 05:08
  • I am specifically trying this logic print(set([i for i in a if (a[i] == a[i+1])). I am aware of many different ways to extract the dups. – RSSregex Apr 17 '19 at 05:09

4 Answers4

5

you could use collections.Counter:

from collections import Counter

a = [1,2,3,2,1,5,6,5,5,5]
c = Counter(a)

res = [n for n, m in c.items() if m > 1]
print(res)  # [1, 2, 5]

this way you iterate once over the list and once over the counter only.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
2

From what I could gather you are trying to implement this logic, this code runs in O(nlogn) time complexity while the code which runs with counter runs in O(n) time complexity means it is faster and cleaner.

a = [1,2,3,2,1,5,6,5,5,5]
a.sort()
print(set([a[i] for i in range(len(a)-1) if (a[i] == a[i+1])]) )

OUTPUT

set([1, 2, 5])
Albin Paul
  • 3,330
  • 2
  • 14
  • 30
1

How about this instead?

a = [1,2,3,2,1,5,6,5,5,5]
duplicates = set(element for element in a if a.count(element) > 1)
print(duplicates)

Output:

{1, 2, 5}
gmds
  • 19,325
  • 4
  • 32
  • 58
0

Suggest a simple solution to find duplicates from List.

>>> a = [1,2,3,2,1,5,6,5,5,5]
>>> a.sort()
>>> set([x for x in a if a.count(x) > 1])

Output: {1, 2, 5}

sameera lakshitha
  • 1,925
  • 4
  • 21
  • 29