I have searched and haven't quite found the same question as mine. I want to remove duplicates from a list of lists in python; however, I don't care what order the values are in the list. They way I am doing it currently is too time-consuming.
What I want to do:
A = [[1,2,3] , [2,3,4] , [3,4,5] , [3,2,4]]
I want to search through A and remove all duplicates. The duplicates here would be [2,3,4] and [3,2,4]. This would reduce down to:
smaller_A = [[1,2,3] , [2,3,4], [3,4,5]]
How I am currently doing it:
todelete = []
for i in range(len(A)):
for j in range(i+1,len(A)):
if set(A[i]) == set(A[j]):
todelete.append(j)
todelete = sorted(set(todelete))
smaller_A= [A[i] for i in range(len(A)) if i not in todelete]
Again, this works, but it is very time consuming when my lists are large. Any ideas? Thanks!
Frozensets are perfect for cases like this, when you need to nest sets:
To convert back to lists, you can do this:
This won't conserve the order of your lists or the elements inside them. (Although it didn't make a difference here.)
If you do need to preserve order, you can iterate over
A
while keeping track of frozensets seen so far:(This isn't optimized; ideally, you'd only call
frozenset(x)
once and store the result in a variable.)you can do a trick with sorting this way
then remove duplicates