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

How do you remove duplicates in a 2d list with the same values but different order

I have a list which contains lists. I am trying to remove any duplicates of lists which may share the same items within the list but in a different order.

for example if I have this

nestedlist=[[1,2,3,4],[4,3,2,1],[1,5,8,7]]

I would like a function that returns something like:

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

[[1,2,3,4],[1,5,8,7]]

>Solution :

Sort the list after acessing sub list and compare accordingly.

new=[]
nestedlist=[[1,2,3,4],[4,3,2,1],[1,5,8,7]]
for i in nestedlist:
    if sorted(i) not in new:
        new.append(i)

print(new)

Gives #

[[1, 2, 3, 4], [1, 5, 8, 7]]         
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