Dict1 = {1: [111, 222], 2: [,]}
Dict2 = {3: [333, 444], 4: [,]}
Merge the two dictionaries to get a similar result.
{1: [111, 222], 2: [333, 444], 3: [,], 4: [,]}
Method 1:
DictMerged1 = dict (dict1.items () + dict2.items ())
Method 2:
DictMerged2 = dict (dict1, ** dict2)
Method 2 is equivalent:
DictMerged = dict1.copy ()
DictMerged. update (dict2)
Or
DictMerged = dict (dict1)
DictMerged. update (dict2)
Method 2 is much faster than method 1. The timeit test is as follows:
$ Python-m timeit-s 'dict1 = dict2 = dict (I, I) for I in range (100) ''dictMerged1 = dict (dict1.items () + dict2.items ())'
10000 loops, best of 3: 20.7 usec per loop
$ Python-m timeit-s 'dict1 = dict2 = dict (I, I) for I in range (100) ''dictMerged2 = dict (dict1, ** dict2 )'
100000 loops, best of 3: 6.94 usec per loop
$ Python-m timeit-s 'dict1 = dict2 = dict (I, I) for I in range (100) ''' dictMerged3 = dict (dict1) ''' dictMerged3. update (dict2 )'
100000 loops, best of 3: 7.09 usec per loop
$ Python-m timeit-s 'dict1 = dict2 = dict (I, I) for I in range (100) ''' dictMerged4 = dict1.copy () ''' dictMerged4. update (dict2 )'
100000 loops, best of 3: 6.73 usec per loop