Frequency of elements in a list

PHOTO EMBED

Mon Mar 08 2021 22:26:17 GMT+0000 (Coordinated Universal Time)

Saved by @randomize_first #python

# finding frequency of each element in a list
from collections import Counter

my_list = ['a','a','b','b','b','c','d','d','d','d','d']
count = Counter(my_list) # defining a counter object

print(count) # Of all elements
# Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})

print(count['b']) # of individual element
# 3

print(count.most_common(1)) # most frequent element
# [('d', 5)]
content_copyCOPY