2

I want to remove both duplicates and permutations from my nested list.

Input:

[[-1, 0, 1], [-1, 1, 0], [-1, 2, -1], [-1, 2, -1], [-1, -1, 2]]

Expected Output:

[[-1, 0, 1], [-1, 2, -1]]

I tried using a list comprehension but I end up with the output as

[[-1, 1, 0], [-1, 2, -1], [-1, 0, 1], [-1, -1, 2]]

Here is what I attempted.

a = [[-1, 0, 1], [-1, 1, 0], [-1, 2, -1], [-1, 2, -1], [-1, -1, 2]]
b_set = set(tuple(x) for x in a)
b = [ list(x) for x in b_set ]
print(b)
0

2 Answers 2

4

The result is expected because [-1, 0, 1] != [-1, 1, 0]. You can sort the inner tuples if you want to make sure that they are considered equal:

b_set = set(tuple(sorted(x)) for x in a)
Sign up to request clarification or add additional context in comments.

Comments

1

Or with map:

b_set = set(map(lambda x: tuple(sorted(x)),a))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.