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

Removing Non-Duplicated Items from List (python)

I have this list:

mylist = [['TX', 'DALLAS'],
 ['TX', 'DALLAS'],
 ['CA', 'LA'],
 ['CA', 'LA'],
 ['ID', 'BOISE']],

I’ve been trying to write a loop that makes a new list containing only those that were duplicates.

This is my current code:

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

for i in mylist:
  if mylist.count(i) > 1:
    mylist.remove(i)

mylist

my output:

[['TX', 'DALLAS'], ['CA', 'LA'], ['ID', 'BOISE']]

So the output would be:

[['TX', 'DALLAS'], ['CA', 'LA']],

>Solution :

list_of_lists = [['TX', 'DALLAS'],
                 ['TX', 'DALLAS'],
                 ['CA', 'LA'],
                 ['CA', 'LA'],
                 ['ID', 'BOISE']]

duplicates = []

for item in list_of_lists:
    if list_of_lists.count(item) > 1 and item not in duplicates:
        duplicates.append(item)

print(duplicates)
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