Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 15, 2020 22:41
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/39de4cc037560c7297d76fd67d6d61e2 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/39de4cc037560c7297d76fd67d6d61e2 to your computer and use it in GitHub Desktop.
from collections import defaultdict
#default_factory attribute is int class.
d=defaultdict(int)
s="defaultdict"
for k in s:
d[k]+=1
print (d)#Output:defaultdict(<class 'int'>, {'d': 2, 'e': 1, 'f': 1, 'a': 1, 'u': 1, 'l': 1, 't': 2, 'i': 1, 'c': 1})
#In normal dict:
d1=dict()
for k in s:
if k not in d1:
d1[k]=1
else:
d1[k]+=1
print (d1)#Output:{'d': 2, 'e': 1, 'f': 1, 'a': 1, 'u': 1, 'l': 1, 't': 2, 'i': 1, 'c': 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment