in a nested list like the one below, I’d like to remove duplicates depending on the position.
[[1,2,3,4], [2,1,3,4], [1,2,3,4], [1,3,2,4]]
So every sub-list contains the same numbers, but in different order. If the order of the numbers is the same, I would like to delete this duplicate. So the list above should look like:
[[1,2,3,4], [2,1,3,4], [1,3,2,4]]
I have tried to write some code by myself, but since I am a beginner, there is no working result. I have tried:
result = []
for i in test_list:
if i not in result:
result.append(i)
return result
Or
tpls = [tuple(x) for x in test_list]
dct = list(dict.fromkeys(tpls))
dup_free = [list(x) for x in test_list]
return dup_free
Thanks!
EDIT2: Sorry everbody, the input was wrong so the code just could not work…
>Solution :
lst = [[1,2,3,4], [2,1,3,4], [1,2,3,4], [1,3,2,4]]
new_lst = []
for a in lst:
if a not in new_lst: # check if current element already in the new_lst or not.
new_lst.append(a)
print(new_lst)
OUTPUT
[[1, 2, 3, 4], [2, 1, 3, 4], [1, 3, 2, 4]]