Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicates nested list Python dependend on position

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading