
Python
Count duplicate elements in a Python list
12/29/2022 · 1 min read
Method 1: dedupe with set, then count
names = ['a', 'b', 'c', 'd', 'c', 'a', 'c']
names_a = list(set(names))
{i: names.count(i) for i in names_a for i in names}Method 2: use collections
import collections
countdic = collections.Counter(['a', 'b', 'c', 'd', 'c', 'a', 'c'])
print(countdic)