Created
January 20, 2018 14:01
-
-
Save hatamiarash7/52e923d9d2a6ea40a0eeba592ed55eb5 to your computer and use it in GitHub Desktop.
Python - Remove duplicate items in list of model objects
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
import toolz | |
class Token(object): | |
def __init__(self, id, token): | |
self.id = id | |
self.token = token | |
def get_id(self): | |
return self.id | |
def get_token(self): | |
return self.token | |
first_list = [ | |
Token(0, 'hi'), | |
Token(1, 'hi'), | |
Token(1, 'hello'), | |
Token(2, 'hi'), | |
Token(3, 'token'), | |
Token(4, 'it'), | |
Token(5, 'in'), | |
Token(6, 'in'), | |
Token(7, 'in'), | |
Token(8, 'hi'), | |
Token(9, 'or'), | |
Token(10, 'hello') | |
] | |
unique_list = toolz.unique(first_list, key=lambda token: token.get_token()) | |
for token in unique_list: | |
print(str(token.get_id()) + ' : ' + token.token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment