Created
October 2, 2018 18:39
-
-
Save knowsuchagency/dbcf4a5cca56c3958d56bc98e3d17282 to your computer and use it in GitHub Desktop.
deduping things
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
deduping stuff | |
""" | |
import itertools as it | |
from operator import itemgetter | |
# using a dict | |
input_ = [ | |
{'id': 1, 'data': 'hello, world'}, | |
{'id': 2, 'data': 'foo'}, | |
{'id': 3, 'data': 'hello, world'} | |
] | |
temp = {} | |
by_data = {temp.get(e['data'], e['data']): e for e in input_} | |
deduped = list(by_data.values()) | |
print(deduped) | |
# using itertools | |
key_func = itemgetter('data') | |
groups = it.groupby(sorted(input_, key=key_func), key=key_func) | |
deduped_2 = list(list(g)[0] for k, g in groups) | |
print(deduped_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment