Skip to content

Instantly share code, notes, and snippets.

@knowsuchagency
Created October 2, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knowsuchagency/dbcf4a5cca56c3958d56bc98e3d17282 to your computer and use it in GitHub Desktop.
Save knowsuchagency/dbcf4a5cca56c3958d56bc98e3d17282 to your computer and use it in GitHub Desktop.
deduping things
"""
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