Given three
dictionaries as follows {key: index}
:
d1 = {'a':0, 'b':1,'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10, 'l':11, 'm':12, 'n':13, 'o':14}
d2 = {'k':0, 'l':1,'m':2, 'n':3, 'o':4, 'p':5, 'q':6, 'r':7, 's':8, 't':9, 'u':10, 'v':11, 'w':12}
d3 = {'a':0, 'b':1,'c':2, 'd':3, 't': 4, 'u': 5, 'v': 6, 'w': 7, 'x': 8, 'y': 9, 'z': 10}
I would like to merge (combine?) them into one, using their unique keys and reindex their values.
Right now, I have the following working but inefficient solution for such small dict
:
%timeit -r 10 -n 10000 d_merged = {key: idx for idx, key in enumerate( sorted(list(set([k for d in [d1, d2, d3] for k in d.keys()]))) ) }
{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
# The slowest run took 4.36 times longer than the fastest. This could mean that an intermediate result is being cached.
# 34.6 µs ± 16.1 µs per loop (mean ± std. dev. of 10 runs, 10000 loops each)
I wonder if there probably exists a better and more efficient approach to do this for large dictionaries (more than 500k elements) without for loops?