def compare_dict(x, y): shared_items = {k: x[k] for k in x if k in y and x[k] == y[k]} differing_values = {k: x[k] for k in x if k in y and x[k] != y[k]} differing_keys = [k for k in x if k not in y] return { "shared_items_in_x": shared_items, "differing_values_in_x": differing_values, "differing_keys_in_x": differing_keys } def is_dict_in_list(d, l): rtn = False for k in l: cd = compare_dict(d, k) if(len(cd['differing_values_in_x']) == 0 and len(cd['differing_keys_in_x']) == 0): rtn = True break return rtn def purify_list_of_dicts(inlist): nlist = [i for j in inlist for i in j] olist = [] for i in range(0, len(nlist)): if is_dict_in_list(nlist[i], olist) == False: olist.append(nlist[i]) return olist
Tuesday, August 23, 2022
Compare two dictionaries in Python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment