3

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?

jpp
  • 159,742
  • 34
  • 281
  • 339
R4444
  • 2,016
  • 2
  • 19
  • 30

1 Answers1

4

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
jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    Might also be worth testing `d = {k: packaged[k] for k in packaged if k in union}`, since this does not create a copy from `items()`. – RoadRunner Dec 17 '18 at 02:34
  • 2
    @RoadRunner, Sure, added, along with an `intersection`-based solution. – jpp Dec 17 '18 at 10:41