Oct-09-2019, 07:07 AM
More or less it's a preference between 1 and 2 and will depend on use case
The third one is tricky as at least first term should be MergeDict, i.e. I think it's overkill
Also note that __add__() can return
Just to add timeing for methods 1 and 2
The third one is tricky as at least first term should be MergeDict, i.e. I think it's overkill
Also note that __add__() can return
self
, no need to do MergeDict(self)
, self
is already instance of MergeDict
classJust to add timeing for methods 1 and 2
# Merging Dictionaries: Different Styles a = {'de': 'Germany'} b = {'sk': 'Slovakia'} c = {'fr': 'France'} from timeit import timeit # Sample-2: Update Method def merge_dicts(diclist): td = {} for d in diclist: td.update(d) return td print(timeit(stmt='md = {**a, **b, **c}', setup='from __main__ import a, b, c', number=100000)) print(timeit(stmt='md = merge_dicts([a, b, c])', setup='from __main__ import a, b, c, merge_dicts', number=100000))
Output:0.05819004499994662
0.24759990400002607
clearly, star unpacking is faster
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs