defaultdict will use more memory than dict.get() since if no key found, defaultdict will set the default value in wrapped dict with the key.
on the other hand, dict.get() just return the default value directly if no key found in dict. No extra space needed.
Running 0.2 billion with empty dict using python2,
200000000 199.584 0.000 283.804 0.000 default.py:11(get_default)
200000000 158.268 0.000 158.268 0.000 default.py:14(get_defaultdict)
The memory usage when running 1 million data with empty dict using python2,
Line # Mem usage Increment Line Contents
================================================
17 10.918 MiB 10.918 MiB @profile
18 def run_get_default():
19 10.918 MiB 0.000 MiB h = {}
20 10.918 MiB 0.000 MiB for i in xrange(0, COUNT):
21 10.918 MiB 0.000 MiB get_default(h, i)
22 10.918 MiB 0.000 MiB get_default(h, i)
Line # Mem usage Increment Line Contents
================================================
24 10.918 MiB 10.918 MiB @profile
25 def run_get_defaultdict():
26 10.918 MiB 0.000 MiB h = defaultdict(int)
27 83.496 MiB 7.273 MiB for i in xrange(0, COUNT):
28 83.496 MiB 58.141 MiB get_defaultdict(h, i)
29 83.496 MiB 7.164 MiB get_defaultdict(h, i)
defaultdict needs a callable value as the dict default value, you can also use the function or lambda to customize your own default value per requirement
dict.get($key, $default) is easier to get the default per record
Thanks