How to remove a substring conditionally from a list of strings?

Advertisements

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?

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. ']

Leave a ReplyCancel reply