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 can i remove duplicate strings from array in python

I have an array in which I’m getting duplicating string that is matching within the other string how can I check and remove from others strings if have

This is the array that I’m getting and I want to remove the duplication string from the array.

"titles": [
        "Financial Analyst",
        "Analyst",
        "Respiratory Therapist",
        "Therapist",
        "Healthcare Specialist",
        "Specialist",
        "Liaison",
        "Business Development Analyst",
        "Development Analyst",
        "Sales Team",
        "Data Analyst",
        "Administrator",
        "Auditor",
        "Director",
        "Director of Events",
        "Controller"
    ]

let me define which I want.
I have two strings in the array
Financial Analyst & Analyst I want to remove the second one from the array because this string comes in the first string.
also like one more example Healthcare Specialist & Specialist the second one I want to remove.
Thanks in advance.

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

>Solution :

As long as optimization isn’t a problem, you can easily achieve this using a for-loop. Here is the code with comments explaining it:

titles = [
        "Financial Analyst",
        "Analyst",
        "Respiratory Therapist",
        "Therapist",
        "Healthcare Specialist",
        "Specialist",
        "Liaison",
        "Business Development Analyst",
        "Development Analyst",
        "Sales Team",
        "Data Analyst",
        "Administrator",
        "Auditor",
        "Director",
        "Director of Events",
        "Controller"
    ]   # Create the example titles array

for i in range(len(titles)-1, -1, -1):    # Loop through the titles from top to bottom
    for j in range(i-1, -1, -1):  # Loop through the titles up until i from top to bottom
        if titles[i] in titles[j]:
            titles.remove(titles[i])

        elif titles[j] in titles[i]:
            titles.remove(titles[j])

print(titles)

Note that you have two if statements so that the order of the array doesn’t matter.

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