Skip to content

Instantly share code, notes, and snippets.

@ChaitanyaBaweja
Last active October 7, 2019 18:30
Show Gist options
  • Save ChaitanyaBaweja/d251489fdcfbd147eba3d450550d5e0e to your computer and use it in GitHub Desktop.
Save ChaitanyaBaweja/d251489fdcfbd147eba3d450550d5e0e to your computer and use it in GitHub Desktop.
# 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)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment