0

Following are two dicts.

In case I have

dict1 = {'mylist' : ['a', 'b', 'c', 'd']}
dict2 = {'mylist' : ['a', 'b', 'c', 'd']}
assert dict1.items() <= dict2.items()   # PASS

and in case I have

dict1 = {'mylist' : ['a', 'b', 'c', 'd']}
dict2 = {'mylist' : ['d', 'c', 'b', 'a']}
assert dict1.items() <= dict2.items()   # FAIL

I needed to sort both lists to be sure the comparison will pass.

I don't care about the items order.

Note that I need to compare dictionaries which only part of the keys values are list and part of them are of other types.

Is there a more straight forward method to compare the dicts and comparison to pass even if the list items are being ordered differently?

Python 3.7

Thanks

dushkin
  • 1,939
  • 3
  • 37
  • 82
  • Why not use dicts as values if you don't care about their ordering ? (note : you'd loose duplicates though) – Paul Bombarde Jun 23 '20 at 15:41
  • 1
    Does this answer your question? [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) – Sebastian Cristi Castillo Jun 23 '20 at 15:42
  • @Bombarde I didn't understand you. Can you share an example please? – dushkin Jun 23 '20 at 16:37

0 Answers0