Given a nested list the task is to remove all duplicates and permutations in that nested list. For example: we have the nested list : [[ -11, 0, 11], [ -11, 11, 0], [ -11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] then output will be {(-11, 0, 11), (-11, -11, 2)}
Using Map
We first sort each sublist to remove any permutations then use the map()function to apply sorted() to each sublist thus converting each sublist into a tuple (to make them hashable) and store them in a set to remove duplicates.
li = [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11],
[-11, 2, -11], [-11, -11, 2], [2, -11, -11]]
# Sorting and removing duplicates
out = set(map(lambda x: tuple(sorted(x)), li))
print(out)
Output
{(-11, 0, 11), (-11, -11, 2)}
Explanation: sorted(x) ensures that permutations of the same elements are treated as equal and set removes duplicates to stores unique, sorted tuples.
Using Set Comprehension
In this method we sort each sublist and convert it into a tuple. By using set comprehension only unique sorted tuples are kept automatically removing duplicates and permutations.
li = [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11],
[-11, 2, -11], [-11, -11, 2]]
# Sorting and removing duplicates using set comprehension
out = {tuple(sorted(x)) for x in li}
print(out)
Output
{(-11, 0, 11), (-11, -11, 2)}Using sort() and "not in" operator
This method involves manually sorting each sublist using the sort() function then iterating through the list to remove duplicates after that we append each unique sorted sublist into a result list by checking if it already exists using the "not in" operator.
input = [[-11, 0, 11], [-11, 11, 0], [-11, 2, -11],
[-11, -11, 2], [2, -11, -11]]
# Sorting sublist elements and removing duplicates
res = []
for i in input:
i.sort()
res.append(i)
output = []
for i in res:
if i not in output:
output.append(i)
output = list(map(tuple, output))
print(tuple(output))
Output
((-11, 0, 11), (-11, -11, 2))
Using operator.countOf() method
This method sorts each sublist and then uses the operator.countOf() method to check the frequency of each sorted sublist in the result list and if the count is zero then it means the sublist is unique and we append it to the result.
import operator as op
input = [[-11, 0, 11], [-11, 11, 0], [-11, 2, -11],
[-11, -11, 2], [2, -11, -11]]
# Sorting sublist elements and removing duplicates using countOf
res = []
for i in input:
i.sort()
res.append(i)
output = []
for i in res:
if op.countOf(output, i) == 0:
output.append(i)
output = list(map(tuple, output))
print(tuple(output))
Output
((-11, 0, 11), (-11, -11, 2))