Count number of occurrences for each element in List in Python
from collections import Counter thelist = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] c = Counter(thelist) print(c)
Output
Counter({'blue': 3, 'red': 2, 'yellow': 1})
from collections import Counter thelist = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] c = Counter(thelist) print(c)
Counter({'blue': 3, 'red': 2, 'yellow': 1})