0

I have got a list l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS'] and another list l2 = ['00001', '00003']. I need to remove the items at index 0 and 2 in the list l1 as it contains the string given in l2. How do I go about doing this?

I have tried the solutions mentioned [here]Is there a simple way to delete a list element by value? and [here]Python: subset elements in one list based on substring in another list, retain only one element per substring but they return an empty list. Thank you!

Navaneeth
  • 9
  • 5

2 Answers2

2

This should work:

l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS']

l2 = ['00001', '00003']

l_result = [x for x in l1 if not any(l in x for l in l2)]  # ['00002YYSSMMYNNN']
Antoine Zambelli
  • 724
  • 7
  • 19
0
# Hello World program i

l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS'] 
l2 = ['00001', '00003']
L3=[]

for el in l2:

  for el1 in l1:
    if el in el1:
     L3.append(el1)
for l3 in L3:
    l1.remove(l3)
print l1
CiroRa
  • 492
  • 3
  • 13