python get frequency of element in list code example
Example 1: how to get frequency of each elements in a python list
lst = ['Sam', 'Sam', 'Tim', 'Tim', 'Tim', 'r', 'l']
freq = {}
counting = [freq.update({x: lst.count(x)}) for x in lst]
{'Sam': 2, 'Tim': 3, 'r': 1, 'l': 1}
Example 2: find frequency of numbers in list python
from collections import Counter
def frequency_table(n):
table = Counter(n)
print('Number\tFrequency')
for number in table.most_common() :
print('{0}\t{1}'.format(number[0], number[1]))