def test_dedupe_list_of_dicts(): list_of_dicts_with_duplicates = [{"a": 123, "b": 1234}, {"a": 3222, "b": 1234}, {"a": 123, "b": 1234}] expected = [{"a": 123, "b": 1234}, {"a": 3222, "b": 1234}] result = dedupe_list_of_dicts(list_of_dicts_with_duplicates) assert expected == result
def test_dedupe_list_of_dicts(): list_of_dicts_with_duplicates = [ {'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}, {'a': 123, 'b': 1234}, ] expected = [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}] result = dedupe_list_of_dicts(list_of_dicts_with_duplicates) assert expected == result
def dedupe_all_lists(obj): """Recursively remove duplucates from all lists.""" squared_dedupe_len = 10 if isinstance(obj, dict): for key, value in obj.items(): obj[key] = dedupe_all_lists(value) return obj elif isinstance(obj, (list, tuple, set)): new_elements = [dedupe_all_lists(v) for v in obj] if len(new_elements) < squared_dedupe_len: new_obj = dedupe_list(new_elements) else: new_obj = dedupe_list_of_dicts(new_elements) return type(obj)(new_obj) else: return obj
def test_dedupe_list_of_dicts(): list_of_dicts_with_duplicates = [ { 'a': 123, 'b': 1234 }, { 'a': 3222, 'b': 1234 }, { 'a': 123, 'b': 1234 }, ] expected = [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}] result = dedupe_list_of_dicts(list_of_dicts_with_duplicates) assert expected == result