Created
April 10, 2015 14:11
-
-
Save Averroes/d2a699cb67662ada71af to your computer and use it in GitHub Desktop.
removing duplicates from a sequence while maintaining order
This file contains hidden or 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
| # example2.py | |
| # | |
| # Remove duplicate entries from a sequence while keeping order | |
| def dedupe(items, key=None): | |
| 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) | |
| if __name__ == '__main__': | |
| a = [ | |
| {'x': 2, 'y': 3}, | |
| {'x': 1, 'y': 4}, | |
| {'x': 2, 'y': 3}, | |
| {'x': 2, 'y': 3}, | |
| {'x': 10, 'y': 15} | |
| ] | |
| print(a) | |
| print(list(dedupe(a, key=lambda a: (a['x'],a['y'])))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment