Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 15, 2020 21:26
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/3a62135a8dbddf45a9d9efdea67c2b49 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/3a62135a8dbddf45a9d9efdea67c2b49 to your computer and use it in GitHub Desktop.
from collections import defaultdict
#default_factory is mentioned as 'int' class.
d=defaultdict(int)
d['a']=1
d['b']=2
#default_factory attribute is int class.
print (d)#Output:defaultdict(<class 'int'>, {'a': 1, 'b': 2})
print (d.get('a'))#Output:1
#get() method -> will return None, if key doesn't exists. __missing__() is not called.
print (d.get('z'))#Output:None
#In regular dictionary
d1={'a':1,'b':2}
#get() method -> returns None , if key doesn't exists.
print (d1.get('a'))#Output:1
print (d1.get('z'))#Output:None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment