Skip to content

Instantly share code, notes, and snippets.

@sweenzor
Created September 26, 2012 03:34
Show Gist options
  • Save sweenzor/3785848 to your computer and use it in GitHub Desktop.
Save sweenzor/3785848 to your computer and use it in GitHub Desktop.
Remove non-unique items from a dictionary, defining uniqueness only between a subset of keys
#!/usr/bin/env python
from itertools import imap, groupby
from operator import itemgetter
def unique_justseen(iterable, key=None):
"""List unique elements, preserving order.
Remember only the element just seen."""
# unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
# unique_justseen('ABBCcAD', str.lower) --> A B C A D
return imap(next, imap(itemgetter(1), groupby(iterable, key)))
if __name__ == '__main__':
print list(unique_justseen('AAAABBBCCDAABBB'))
test = [
{'a': 3, 'b': 4, 'c': 1},
{'a': 3, 'b': 4, 'c': 2},
{'a': 3, 'b': 4, 'c': 3},
{'a': 3, 'b': 4, 'c': 4},
{'a': 3, 'b': 4, 'c': 5},
{'a': 3, 'b': 4, 'c': 6},
{'a': 3, 'b': 4, 'c': 7}
]
print list(unique_justseen(test, key=lambda x: (x['a'], x['b'])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment