-1

I have 2 dictionaries that are supposed to be exactly the same but there was a small difference, around 0.0035% but still that's a considerable number of elements given my number of data.

What i'd like is to get that difference between 2 dictionaries in a 3 rd dictionary, for example :

mydict = {
    'a': [[2,4], [5,6]],
    'b': [[1,1], [1,7,9], [6,2,3]],
    'c': [['a'], [4,5]],
}

second_dict = {'a': [[2,4]],
        'b': [[1,1], [1,7,9], [6,2,3]],

    }

Let's assume mydict is the ground truth with all the elements and second_dict is the one missing a few, diff_dict should be :

 diff_dict = {'a': [[5,6]],
            'c': [['a'], [4,5]],    
        }
Talar
  • 53
  • 1
  • 7
  • What have you tried so far? The idea is probably to run over all keys of `mydict` and check if they exist in `second_dict` or not. If they do, then the values need to be compared (are they always lists of lists of numbers?) – FlyingTeller Feb 02 '18 at 07:41
  • Python `dict`s may not be the best way to hold large amounts of data, and differences will be relatively expensive to calculate. Are you using another library, such as Pandas or Numpy? If so, edit your question and maybe you might get a more performant answer. – Hameer Abbasi Feb 02 '18 at 08:01
  • what i tried and didn't work `value = {k: my_dict[k] for k in set(my_dict) - set(plus_min_dict)}` – Talar Feb 02 '18 at 08:02
  • 1
    Possible duplicate of [How to get the difference between two dictionaries in Python?](https://stackoverflow.com/questions/32815640/how-to-get-the-difference-between-two-dictionaries-in-python) – Edwin van Mierlo Feb 02 '18 at 08:10
  • What do you mean by "differences"? Just the keys? Or is it also possible that a key is there but with different values? – ChatterOne Feb 02 '18 at 08:21
  • Keys and possible difference,difference being missing element of a key in second_dict – Talar Feb 02 '18 at 08:26

2 Answers2

2
>>> diff_dict = {k:[x for x in v if x not in second_dict.get(k, [])] for k,v in mydict.items()}
>>> diff_dict
{'c': [['a'], [4, 5]], 'a': [[5, 6]], 'b': []}
>>> diff_dict = {k:v for k,v in diff_dict.items() if v}
>>> diff_dict
{'c': [['a'], [4, 5]], 'a': [[5, 6]]}

I would recommend the having the values in mydict and second_dict as set instead of list for efficiency and cleaner code.

shanmuga
  • 4,329
  • 2
  • 21
  • 35
0

This works, but is not as efficient as possible since it repeats the second_dict.get() for every entry in mydict:

mydict = {
    'a': [[2,4], [5,6]],
    'b': [[1,1], [1,7,9], [6,2,3]],
    'c': [['a'], [4,5]],
}
second_dict = {
    'a': [[2,4]],
    'b': [[1,1], [1,7,9], [6,2,3]],
}
diff_dict = {
    k: [v for v in vals if v not in second_dict.get(k, [])]
    for k, vals in mydict.items()
}
# only keep non-empty lists
diff_dict = {k: v for k, v in diff_dict.items() if v}
diff_dict
# {'a': [[5, 6]], 'c': [['a'], [4, 5]]}

This is harder to read but a little more efficient:

diff_dict = {
    k: (
        [v for v in vals if v not in second_dict[k]] if k in second_dict 
        else vals
    ) 
    for k, vals in mydict.items()
}
diff_dict = {k: v for k, v in diff_dict.items() if v}

If you are willing to keep your items in sets of tuples instead of lists of lists, you could do this much more easily and efficiently:

mydict = {
    'a': {(2,4), (5,6)},
    'b': {(1,1), (1,7,9), (6,2,3)},
    'c': {('a',), (4,5)},
}
second_dict = {
    'a': {(2,4)},
    'b': {(1,1), (1,7,9), (6,2,3)},
}
diff_dict = {key: full - second_dict.get(key, set()) for key, full in mydict.items()}
diff_dict
# {'a': set([(5, 6)]), 'c': set([(4, 5), ('a',)]), 'b': set([])}
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45