I have created a default dictionary as below :
from collections import defaultdict
dd = defaultdict(lambda : "Key not found")
dd = {'a':1,'b':2}
print(dd)
print(dd['a']) # Prints 1
print(dd['c']) # Throws KeyError
But, the below code snippet works :
from collections import defaultdict
(lambda : "Key not found")
dd['a']=1
dd['b']=2
print(dd)
print(dd['a']) # Prints 1
print(dd['c']) # Prints "Key not found"
Could anyone please explain me why the first code snippet throws error whereas the second one runs fine as expected..