Home > other >  How to find the duplicates in a list and create another list with them ? with answer
How to find the duplicates in a list and create another list with them ? with answer

Time:02-11

My own Solution:

list1=[1,2,3,4,4,5,5,5,6,6,7,8,8,8,8]
counter=0
for j in list(set(list1)):
    for i in list1:
        if j==i:
            counter=counter 1
    print(j,"have",counter,"duplicates")
    counter=0

give some suggestion

CodePudding user response:

Using collections.Counter:

>>> list1 = [1,2,3,4,4,5,5,5,6,6,7,8,8,8,8]
>>> from collections import Counter
>>> [(i, count) for i, count in Counter(list1).items() if count > 1]
[(4, 2), (5, 3), (6, 2), (8, 4)]
  • Related