2

I need to compare dictionaries in Python and I wrote this code below:

diff_dict = {}
for key, value in old_dictionary.items():
    for k, v in new_dictionry.items():
        if k==key:
            inner_dict = {}
            for key1, value1 in value.items():
                for k1, v1 in v.items():
                    if key1==k1 and value1!=v1:
                        compared_value = str(value1) + '_' + str(v1)
                        inner_dict.update({key1: compared_value})
            diff_dict.update({k: inner_dict})

I have some sites as key, parameter as inner keys and value for each parameter. I need to compare the current dictionary with the dictionary from the previous day.

But it is not efficient, it takes half an hour and still, it's not done. Is there any efficient way to do this?

I want to get a new nested dict with a structure like this:

{key: {inner_key: compared_value}}

Compared value is concatenated new and old values so I can later split them by '_' and make two columns in Pandas.

example of data old:

{'site-1/cell-1': {'tac' : 10, md: 5},
 'site-2/cell-1': {'tac' : 10, md: 1}}

new:

{'site-1/cell-1': {'tac' : 10, md: 4},
 'site-2/cell-1': {'tac' : 12, md: 1}}

Desired output:

{{'site-1/cell-1': {md: '5_4'},
 'site-2/cell-1': {'tac' : '10_12'}}
3
  • Use the O(1) lookup property of dictionaries! Don't just iterate over all pairs of items. Commented Feb 13, 2019 at 14:09
  • can you post an example data and expected output ? Commented Feb 13, 2019 at 14:22
  • @MehrdadPedramfar I just edited my question :) Commented Feb 13, 2019 at 14:28

2 Answers 2

2

The following should work and be significantly faster than yours:

from collections import defaultdict


diff_dict = defaultdict(dict)
for key, value in old_dictionary.items():
    v = new_dictionary.get(key, None)
    if v:
        for key1, value1 in value.items():
            v1 = v.get(key1, None)
            if v1 and value1 != v1:
                compared_value = str(value1) + '_' + str(v1)
                diff_dict[key].update({key1: compared_value})

Also, FYI, proving a minimal example with input and desired output would really make the lives of those who want to help easier. Keep that in mind for next time.

I have created one myself:

old_dictionary = {'a': {'b': 1, 'c': 2}, 'd': {'f':5}}
new_dictionary = {'a': {'b': 2, 'c': 2}, 'd': {'f':6}}

which produces:

defaultdict(<class 'dict'>, {'a': {'b': '1_2'}, 'd': {'f': '5_6'}})

and if the defaultdict datatype at the end confuses you, you can always convert to a vanilla-dict simply by casting:

diff_dict = dict(diff_dict)
Sign up to request clarification or add additional context in comments.

3 Comments

I have edited my question. :) With your output I am getting empty inner dictionary
can you give it another go? I get defaultdict(<class 'dict'>, {'site-1/cell-1': {'md': '5_4'}, 'site-2/cell-1': {'tac': '10_12'}}) for your example.
Yeah, I think this works. :) Thank you very much. :)
1

I'm not 100% sure this is what you want based on your question. But if you want to concatenate the "inner values" then you can use this:

diff_dict = {k_out: {k_in: f'{v_in}-{new_dictionary[k_out][k_in]}' for k_in, v_in in v_out.items()} for k_out, v_out in old_dictionary.items()}

Without using a one-line generator:

diff_dict = {}
for k_out, v_out in old_dictionary.items():
    for k_in, v_in in v_out.items():
        diff_dict.update({k_out: {k_in: f'{v_in}-{new_dictionary[k_out][k_in]}'}})

If you're ever not sure if a key exists, you can replace dictionary[key] with dictionary.get(key, ""). This will return a blank string if there is not a key, value pair at the key you give.

For inputs of:

old_dictionary = {'a': {'z': 111}, 'b': {'y': 999}}
new_dictionary = {'a': {'z': 444}, 'b': {'y': 777}}

This gives:

{'a': {'z': '111-444'}, 'b': {'y': '999-777'}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.