count frequency of string in list python code example

Example 1: python count the frequency of words in a list

from collections import Counter
list1=['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)

print(counts)
# Counter({'apple': 3, 'egg': 2, 'banana': 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]))
        
# src : Doing Math With Python