8 examples of 'compare two dictionaries in python' in Python

Every line of 'compare two dictionaries in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
28def _compare_dicts(one, two):
29 if len(one) != len(two):
30 return False
31
32 for key, value in one.items():
33 if key not in one or key not in two:
34 return False
35
36 if not compare_schemas(one[key], two[key]):
37 return False
38 return True
1287def _compare_dictionary_lists(self, list_a, list_b):
1288 '''
1289 If all of list_a exists in list_b, return True
1290 '''
1291 if not isinstance(list_a, list) or not isinstance(list_b, list):
1292 return False
1293 matches = 0
1294 for dict_a in list_a:
1295 for dict_b in list_b:
1296 if self._compare_dicts(dict_a, dict_b):
1297 matches += 1
1298 break
1299 result = (matches == len(list_a))
1300 return result
19def deep_compare(obj1, obj2):
20 """
21 >>> deep_compare({'1': None}, {})
22 False
23 >>> deep_compare({'1': {}}, {'1': None})
24 False
25 >>> deep_compare({'1': [1]}, {'1': [2]})
26 False
27 >>> deep_compare({'1': 2}, {'1': '2'})
28 True
29 >>> deep_compare({'1': {'2': [3, 4]}}, {'1': {'2': [3, 4]}})
30 True
31 """
32
33 if set(list(obj1.keys())) != set(list(obj2.keys())): # Objects have different sets of keys
34 return False
35
36 for key, value in obj1.items():
37 if isinstance(value, dict):
38 if not (isinstance(obj2[key], dict) and deep_compare(value, obj2[key])):
39 return False
40 elif str(value) != str(obj2[key]):
41 return False
42 return True
310def equal_dictionary(dict1, dict2):
311 if dict1 is not None and dict2 is not None:
312 dict1_keys = dict1.keys()
313 if equal_lists(dict1_keys, dict2.keys()):
314 for key in dict1_keys:
315 if dict1[key] != dict2[key]:
316 return False
317 return True
318 return False
348def _recursive_compare(v1, v2):
349 '''
350 Return v1 == v2. Compares list, dict, recursively.
351 '''
352 if isinstance(v1, list):
353 if v2 is None:
354 v2 = []
355 if len(v1) != len(v2):
356 return False
357 v1.sort(key=_id_or_key)
358 v2.sort(key=_id_or_key)
359 for x, y in zip(v1, v2):
360 if not _recursive_compare(x, y):
361 return False
362 return True
363 elif isinstance(v1, dict):
364 if v2 is None:
365 v2 = {}
366 v1 = dict(v1)
367 v2 = dict(v2)
368 if sorted(v1) != sorted(v2):
369 return False
370 for k in v1:
371 if not _recursive_compare(v1[k], v2[k]):
372 return False
373 return True
374 else:
375 return v1 == v2
13def compare_dict(self, a, b):
14 fa = set('{}={}'.format(k, v) for k, v in flatten(a));
15 fb = set('{}={}'.format(k, v) for k, v in flatten(b));
16
17 errors = len(fa - fb) + len(fb - fa)
18
19 if errors:
20 print("=== ERRORS ===")
21
22 if len(fa - fb):
23 print("In b but not a")
24 for e in sorted(fa - fb):
25 print(' ', e)
26
27 if len(fb - fa):
28 print("In a but not b")
29 for e in sorted(fb - fa):
30 print(' ', e)
31
32 self.assertEqual(0, errors)
120def _compare_lists(*args):
121 """
122 Compare two or more lists. Return True if all equals.
123 """
124
125 if not args:
126 return True
127 compared_item = set(args[0])
128 for item in args[1:]:
129 if compared_item != set(item):
130 return False
131 return True
71def _subdict(dict1, dict2):
72 """True if dict1 <= dict2
73 All items in dict1 must be in dict2.
74 """
75 for key, val in dict1.items():
76 if key not in dict2 or dict2[key] != val:
77 return False
78 return True

Related snippets