Python,

Insha.
May 6, 2021

Different between Dictionary and DefaultDictionary.

Dictionary is direct data type in python, whereas Defaultdictionary is a function in collections module.

If the key / item not present in dictionary then we get Error message whereas in case of defaultdictionary we will not get error instead we will get the default assigned value only thing is we have to define while creating defaultdictionary variable.

#dictinary and defaultdictnormaldictionary=dict()
print(type(normaldictionary))
#print(normaldictionary[“keynotexisit”])
#Above normal dictionary give an error as key not present
from collections import defaultdict
defaultdict1=defaultdict()
print(type(defaultdict1))
#print(defaultdict1[‘keynotexisit’])
######################################
from collections import defaultdict
default2=defaultdict(int)
print(default2[‘keynotexist’])

#Execute the above code all thing will be clear.

Thank you.

--

--