Skip to content

Instantly share code, notes, and snippets.

@Ogreman
Created October 22, 2014 11:21
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 Ogreman/5da3cecf458216157780 to your computer and use it in GitHub Desktop.
Save Ogreman/5da3cecf458216157780 to your computer and use it in GitHub Desktop.
deduper with history of duplicates and support for hashable types
from collections import deque
def dedupe_with_history(items, key=None, history=None):
previous = deque(maxlen=history)
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val)
else:
previous.append(item)
yield previous
"""
>> list(dedupe_with_history([5, 1, 1, 3, 66, 8, 66])
[5, 1, 3, 66, 8, deque([1, 66])]
>> *items, list(dupes) = dedupe_with_history([5, 1, 1, 3, 66, 8, 66])
>> items
[5, 1, 3, 66, 8]
>> dupes
[1, 66]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment