I am trying to complete a coding challenge where I have to remove duplicates of tuples in a list.
Right now, the .remove() method seems to be removing both my (1,1) tuples and not the (2,2) tuple.
def remove_duplicates(lis):
for i in range(len(lis)):
print(f"We are at i = {i}")
for j in range(i + 1, len(lis)):
print(f"j={j}")
print(f"This is lis[i] {lis[i]}")
print(f"This is lis[j] {lis[j]}")
if lis[i] == lis[j]:
to_remove = lis[j]
lis.reverse() # reversal is done to remove the last matching element
lis.remove(lis[j])
lis.reverse()
print(lis)
return lis
lis = [(1,1), (2,2), (3,3), (2,2), (1,1)]
remove_duplicates(lis) # should return [(1,1), (2,2), (3,3)]
OUTPUT