Default-Dict
Sun 29 June 2025
from collections import defaultdict
user = defaultdict(lambda: 'Kevin')
user
defaultdict(<function __main__.<lambda>()>, {})
type(user)
collections.defaultdict
user['abc']
'Kevin'
user['name'] = 'Peter'
user['age'] = 21
user['city'] = 'Toronto'
user
defaultdict(<function __main__.<lambda>()>,
{'abc': 'Kevin', 'name': 'Peter', 'age': 21, 'city': 'Toronto'})
defaultdict(<function __main__.<lambda>>,
{'abc': 'Kevin',
'age': 21,
'city': 'Toronto',
'country': 'Kevin',
'name': 'Peter'})
Cell In[8], line 1
defaultdict(<function __main__.<lambda>>,
^
SyntaxError: invalid syntax
from collections import defaultdict
d = defaultdict(lambda: 'default value', {
'abc': 'Kevin',
'age': 21,
'city': 'Toronto',
'country': 'Kevin',
'name': 'Peter'
})
for k, v in user.items():
print(k, "==>", v)
abc ==> Kevin
name ==> Peter
age ==> 21
city ==> Toronto
Score: 10
Category: basics