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 to remove a substring conditionally from a list of strings?

I have the following lists of strings:

my_list1 = [' If the display is not working ', "<xxx - tutorial section>", "tutorial section", ' display message appears. ']

my_list2 = [' If the display is not working ', "tutorial section", "<xxx - tutorial section>", ' display message appears. ']

How can I identify a string that is equal to a substring right after or before - surrounded by < and > symbols. In my example, this is tutorial section. I want to remove this string to get the following results:

my_list1_ed = [' If the display is not working ', "<xxx - tutorial section>", ' display message appears. ']

my_list2_ed = [' If the display is not working ', "<xxx - tutorial section>", ' display message appears. ']

If a list does not contain this kind of duplicates, then nothing should be done. How can I implement this logic?

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

My attempt:

final_list = []
lists = my_list1 + my_list2
for i, v in enumerate(lists):
   if (v not in lists[i-1]) or (v not in lists[i+1]):
       final_list.append(v)

The result that I get (duplicates still are there):

[' If the display is not working ',
 '<xxx - tutorial section>',
 'tutorial section',
 ' display message appears. ',
 ' If the display is not working ',
 'tutorial section',
 '<xxx - tutorial section>',
 ' display message appears. ']

>Solution :

Changed the code a bit, see if helps.

final_list = []
lists = my_list1 + my_list2

for i, v in enumerate(lists):
    if i == 0:
        if v not in lists[i+1]:
            final_list.append(v)
    elif i == len(lists) - 1:
        if v not in lists[i-1]:
            final_list.append(v)
    else:
        if (v not in lists[i-1]) and (v not in lists[i+1]):
            final_list.append(v)

print(final_list)

output: [' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ', ' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']

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