-
-
Save IndhumathyChelliah/39de4cc037560c7297d76fd67d6d61e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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