How to find all duplicate elements in a list using Python

3 Answers

0 votes
import collections

lst = [1, 1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7]

print([item for item, count in collections.Counter(lst).items() if count > 1])


'''
run:

[1, 4, 6, 7]

'''

 



answered Feb 18, 2019 by avibootz
0 votes
import collections

lst = [1, 1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7]

duplicates = [item for item, count in collections.Counter(lst).items() if count > 1]

print(duplicates)


'''
run:

[1, 4, 6, 7]

'''

 



answered Feb 18, 2019 by avibootz
0 votes
import collections

lst = [1, 1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7]

count_elements = {i:lst.count(i) for i in lst}

for key, value in count_elements.items():
  if (value > 1):
    print(key)


'''
run:

1
4
6
7

'''

 



answered Feb 18, 2019 by avibootz

Related questions

1 answer 134 views
1 answer 173 views
1 answer 153 views
2 answers 213 views
1 answer 191 views
1 answer 162 views
...