DefaultDict

Run Settings
LanguagePython
Language Version
Run Command
from collections import defaultdict ##### USING INDUILT FUNCTION ##### default_dict1 = defaultdict(int, {'a':10, 'b':20}) print(default_dict1['a']) # 10 print(default_dict1['b']) # 20 print(default_dict1['c']) # 0 print(default_dict1['z']) # 0 # **NOTE as int() returns the default value of integer type, i.e., 0 ##### USING LAMBDA ##### default_dict2 = defaultdict(lambda: 0, {'a':10, 'b':20}) print(default_dict2['a']) # 10 print(default_dict2['b']) # 20 print(default_dict2['c']) # 0 print(default_dict2['z']) # 0 ##### USING LAMBDA 2 ##### default_dict3 = defaultdict(lambda: "Key doesn't exists", {'a':'apple', 'b':'ball'}) print(default_dict3['a']) # apple print(default_dict3['b']) # ball print(default_dict3['c']) # Key doesn't exists print(default_dict3['z']) # Key doesn't exists
Editor Settings
Theme
Key bindings
Full width
Lines