Remove duplicate tuples with same elements in python nested list

I have a list of tuples and I need to delete tuples containing the same elements.

q = [(1.0), (2.3), (3.2), (0.1)]

OutputRequired = [(1,0), (2,3)] Output order does not matter

the command set () does not work as expected.

+3


source to share


1 answer


In this solution, I copy each of the tuples in temp

after checking if it is present in temp

, and then copy it back to d

.

d = [(1,0),(2,3),(3,2),(0,1)]
temp = []
for a,b in d :
    if (a,b) not in temp and (b,a) not in temp: #to check for the duplicate tuples
        temp.append((a,b))
d = temp * 1 #copy temp to d

      



This will give the result as expected.

+3


source







All Articles