10 examples of 'python compare two dictionaries with same keys' in Python

Every line of 'python compare two dictionaries with same keys' 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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
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
112def __eq__(self, other):
113 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
114 while comparison to a regular mapping is order-insensitive.
115
116 '''
117 if isinstance(other, SortedDict):
118 return dict.__eq__(self, other) and all(_imap(_eq, self, other))
119 return dict.__eq__(self, other)
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
436def _dict_diff(a, b):
437 """A one way dictionary diff.
438
439 a: a dictionary
440 b: a dictionary
441
442 Returns: True if the dictionaries are different
443 """
444 # Only things the source has which the target lacks matter
445 if set(a.keys()) - set(b.keys()):
446 LOG.debug('metadata diff -- source has extra keys: %(keys)s',
447 {'keys': ' '.join(set(a.keys()) - set(b.keys()))})
448 return True
449
450 for key in a:
451 if str(a[key]) != str(b[key]):
452 LOG.debug('metadata diff -- value differs for key '
453 '%(key)s: source "%(source_value)s" vs '
454 'target "%(target_value)s"',
455 {'key': key,
456 'source_value': a[key],
457 'target_value': b[key]})
458 return True
459
460 return False
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
77def combine_dicts(dict1, dict2):
78 return dict(dict1.items() + dict2.items())
5def _deep_dict_eq(d1, d2):
6 k1 = sorted(d1.keys())
7 k2 = sorted(d2.keys())
8 if k1 != k2: # keys should be exactly equal
9 return False
10 return sum(deep_eq(d1[k], d2[k]) for k in k1) == len(k1)

Related snippets