0

I have two dictionaries, for example:

first = { 'test' :  [1, 2, 3] }
second = { 'test' :  [3, 2, 1] }

first == second  # False

Is there a way to compare these two dictionaries and ignore the order of the values in the lists inside?

I tried using OrderedDict but it didn't work:

from collections import OrderedDict

first = OrderedDict({ 'test' :  [1, 2, 3] })
second = OrderedDict({ 'test' :  [3, 2, 1] })

first == second  # False
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
  • 2
    `[1, 2, 3]` is not the same as `[3, 2, 1]`. If you don't care about the order, you should probably not use lists. OrderedDict is about ordering the keys, not values, which would make you problem even worse, since you don't care about the order of the keys either. – zvone Oct 25 '20 at 10:11
  • What would the equality criteria for the "list"? For example is [1, 2, 3] equals to [1, 2, 3, 3]? – Dani Mesejo Oct 25 '20 at 10:12
  • Why *would* OrderedDict work? That just means the dictionary itself is ordered, it doesn't order the keys or values (`OrderedDict(test=[3, 2, 1]) == {"estt": [1, 2, 3]}` would be a very unexpected result). – jonrsharpe Oct 25 '20 at 10:13
  • I don't know what MongoDB supports, and I don't know what you are doing in general, but if you actually need a set, you should be using a set. If that can't go into MongoDB, you can convert when communicating with the data base. – zvone Oct 25 '20 at 10:15
  • you may have a look at [How to efficiently compare two unordered lists (not sets) in Python?](https://stackoverflow.com/questions/7828867/how-to-efficiently-compare-two-unordered-lists-not-sets-in-python) – adamkwm Oct 25 '20 at 10:17

3 Answers3

1

Use Counters of your lists (or sets if you have [1,2] == [2,1,2]) for your contained lists:

from collections import Counter

first = { 'test' :  [1, 2, 3] }
second = { 'test' :  [3, 2, 1] }
third = { 'test' :  [3, 2, 1], "meh":"" }

def comp_dict(d1,d2):
    return d1.keys() == d2.keys() and all(Counter(d1[k]) == Counter(d2[k]) for k in d1)
    # or 
    # return d1.keys() == d2.keys() and all(set(d1[k]) == set(d2[k]) for k in d1)

print(comp_dict(first, second))
print(comp_dict(first, third))

Output:

True
False
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

I found this solution:

import unittest

class TestClass(unittest.TestCase):

    def test_count_two_dicts(self, d1, d2):
        self.assertCountEqual(d1, d2)

if __name__ == '__main__':
    first = { 'test' :  [1, 2, 3] }
    second = { 'test' :  [3, 2, 1] }
    
    t = TestClass()
    t.test_count_two_dicts(d1=first, d2=second)
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
0

If you want to compare both dicts (True or False)

first = { 'test' : [1, 2, 3] }

second = { 'test' : [3, 2, 1] }

is_equal = first == second

is_equal
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
  • This would not help me because it'll return `False` and I want the output to be `True` without considering the order of the inner lists. – Alon Barad Oct 27 '20 at 08:54