Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 22, 2020 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/3bf7a2cf3a5abf0a15f9721467abdd13 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/3bf7a2cf3a5abf0a15f9721467abdd13 to your computer and use it in GitHub Desktop.
#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