Compare a dictionary with other and select only those keys which are present in the other

Viewed 60

I have two dictionaries:

union = {'a':'gamma','b':'beta','d':'theta'}
packaged = {'a':'alpha','b':'gamma','c':'alpha'}

I want:

packaged = {'a': 'alpha', 'b': 'gamma'}

Thus, I want to select only those keys from packaged which are present in union, along with their values.

I read this similar question and I am doing -

for k, v in list(packaged.items()):
    if k not in union.keys():
        del packaged[k]
print(packaged)

Which gives me the desired answer.

Is my method the most faster/ efficient way?, if not - is there any faster/ efficient way of doing this?

1 Answers

You can use a dictionary comprehension. This creates a new dictionary but has the same complexity as your for loop:

d = {k: v for k, v in packaged.items() if k in union}

Here is some realistic benchmarking, along with two slightly more efficient variants:

union = {'a':'gamma','b':'beta','d':'theta', **dict.fromkeys(range(5000, 10000))}
packaged = {'a':'alpha', 'b':'gamma', 'c':'alpha', **dict.fromkeys(range(1, 15000))}

def dct_cmp(union, packaged):
    return {k: v for k, v in packaged.items() if k in union}

def dct_cmp_from_key(union, packaged):
    return {k: packaged[k] for k in packaged if k in union}

def dct_cmp_from_key_intersect(union, packaged):
    return {k: packaged[k] for k in set(packaged) & set(union)}

def loopy(union, packaged):
    for k, v in list(packaged.items()):
        if k not in union:
            del packaged[k]
    return packaged

assert dct_cmp(union, packaged.copy()) == loopy(union, packaged.copy())
assert dct_cmp(union, packaged.copy()) == dct_cmp_from_key(union, packaged.copy())
assert dct_cmp(union, packaged.copy()) == dct_cmp_from_key_intersect(union, packaged.copy())

%timeit dct_cmp(union, packaged.copy())                     # 1.94 ms
%timeit dct_cmp_from_key(union, packaged.copy())            # 1.8 ms
%timeit dct_cmp_from_key_intersect(union, packaged.copy())  # 1.8 ms
%timeit loopy(union, packaged.copy())                       # 2.75 ms
Related