1

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.

asked Apr 20, 2022 at 0:04
3
  • what about "Director" and "Director of Events" ? Delete the 2nd one or not ? Commented Apr 20, 2022 at 0:20
  • Is it correct to assume that if a word appears in another, the single word should be removed and the composed not ? Commented Apr 20, 2022 at 0:28
  • @Rabinzel I want to remove single which are coming in the other string. Thanks Commented Apr 20, 2022 at 0:42

2 Answers 2

2

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.

answered Apr 20, 2022 at 0:21
0
2

A very similar approach with a list comprehension, removing members that are inside any other string in the titles list.

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"
 ]
titles = [title for title in titles if not any([title != _title and title in _title for _title in titles])]

Result:

[
 "Financial Analyst",
 "Respiratory Therapist",
 "Healthcare Specialist",
 "Liaison",
 "Business Development Analyst",
 "Sales Team",
 "Data Analyst",
 "Administrator",
 "Auditor",
 "Director of Events",
 "Controller"
]
answered Apr 20, 2022 at 2:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.