Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active Jul 22, 2020
Embed
What would you like to do?
#finding the number of occurrences of each character in the string
s="dictionary"
d={n:s.count(n) for n in s}
print (d)#Output:{'d': 1, 'i': 2, 'c': 1, 't': 1, 'o': 1, 'n': 1, 'a': 1, 'r': 1, 'y': 1}
#finding the number of occurrences of each word in list using dict comprehension
l=["red","green","blue","red"]
d={n:l.count(n) for n in l}
print (d)#Output:{'red': 2, 'green': 1, 'blue': 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment