1

I have found multiple posts (this one too) that was about this topic but none of those options would work for my dictionaries. I wanna compare my dictionnaries and know the number of values identical and also the pair of key-values unique to each dictionaries.

I am working with two dictionaries with Tuples as key and list as value (where the second value is another list) as follow:

Dict1:{(10, 11): ['C', ['T']],
       (20, 21): ['C', ['T']],
       (34, 35): ['G', ['A']],
       (68, 69): ['A', ['T','G']]}


Dict2:{(10, 11): ['C', ['T']],
       (20, 21): ['C', ['A']],
       (40, 41): ['T', ['G']],
       (68, 69): ['A', ['T','G']]}

and I would like to compare those dictionnary and have different output. Using my example that's the variable I would like to have:

  • 2 values are identical and present in both dict
  • 2 values are only in dict1
  • 2 values are only in dict2

I was about to loop over dict1 and compare each key to all dict2 each time (and having variables that i'll updates each time a condition is met) but I am aware that it is probably not the most efficient way of doing it.

Does anyone have a quicker idea ?

Thanks

Roselia99
  • 13
  • 3

1 Answers1

0

You can first filter out the keys using set-like methods of dict.keys() objects, and then proceed to get the pairs:

>>> same_keys = Dict1.keys() & Dict2.keys()
>>> dict1_unq_keys = Dict1.keys() - Dict2.keys()
>>> dict2_unq_keys = Dict2.keys() - Dict1.keys()
>>> same_pairs = [(key, Dict1[key]) for key in same_keys if Dict1[key] == Dict2[key]]
>>> Dict1_unq_pair = [(key, Dict1[key]) for key in dict1_unq_keys]
>>> Dict2_unq_pair = [(key, Dict2[key]) for key in dict2_unq_keys]

>>> same_pairs
[((68, 69), ['A', ['T', 'G']]), ((10, 11), ['C', ['T']])]

>>> Dict1_unq_pair
[((34, 35), ['G', ['A']])]

>>> Dict2_unq_pair
[((40, 41), ['T', ['G']])]

Note:

If it is possible for you to use tuples as dict values instead of lists, this could be done more easily, by directly using dict.items() set operations to get the same pairs.

For example, if Dict1 and Dict2 were of the following form:

>>> Dict1
{(10, 11): ('C', ('T',)),
 (20, 21): ('C', ('T',)),
 (34, 35): ('G', ('A',)),
 (68, 69): ('A', ('T', 'G'))}

>>> Dict2
{(10, 11): ('C', ('T',)),
 (20, 21): ('C', ('A',)),
 (40, 41): ('T', ('G',)),
 (68, 69): ('A', ('T', 'G'))}

# Then you could simple do:
>>> same_pairs = list(Dict1.items() & Dict2.items())

>>> same_pairs
[((68, 69), ('A', ('T', 'G'))), ((10, 11), ('C', ('T',)))]
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • Thank you very much. I did change my dictionnaries with tuples as suggested and it works fine. However, if i wanna get the pairs unique to dict 1 using the second method, I did try to wrote something like that but without success `OnlyDict1 = list(dict1.items() & not in dict2.items())` – Roselia99 Feb 26 '21 at 11:36
  • You can try `dict1.items() - dict2.items()`, this will give pairs that are unique to dict1. However, eventhough the pair may be unique, they will be unique because of different keys, and may have same values. If you want pairs where both key and value are unique, you would need to use `dict1.keys() - dict2.keys()` to get the keys, and then find their values in a loop. – Sayandip Dutta Feb 26 '21 at 11:39