So I was working on a problem in which, given a list of tuples, I had to return a new list only containing the elements in which the first element of the tuple isn't duplicated. I had no problem removing duplicates but my problem is removing the original element as soon as a duplicate is found. eg.
inputList = [("test0", 20), ("test1", 25), ("test0", 30)]
This should return
[("test1", 25)]
I have after my troubles gotten it to work, however I fear my code is bad and that there is a much easier way to perform what I have done. I've done it by first removing duplicates
visited = set()
marked = []
output = []
for key, value in resList:
if not key in visited:
visited.add(key)
output.append((key, value))
else:
marked.append(key)
I then check my new output list against my marked list
resList = []
for mark in marked:
for i in range(len(output)):
if mark != output[i][0]
resList.append(output[i])