L P
L P

Reputation: 1846

How to remove an array containing certain strings from another array in Python

Example:

a = ['abc123','abc','543234','blah','tete','head','loo2']

So I want to filter out from the above array of strings the following array b = ['ab','2']

I want to remove strings containing 'ab' from that list along with other strings in the array so that I get the following:

a = ['blah', 'tete', 'head']

Upvotes: 0

Views: 96

Answers (3)

isaach1000
isaach1000

Reputation: 1839

newA = []
for c in a:
    for d in b:
        if d not in c:
            newA.append(c)
            break
a = newA

Upvotes: 1

dawg
dawg

Reputation: 104111

>>> a = ['abc123','abc','543234','blah','tete','head','loo2']
>>> b = ['ab','2']
>>> [e for e in a if not [s for s in b if s in e]]
['blah', 'tete', 'head']

Upvotes: 2

TerryA
TerryA

Reputation: 60024

You can use a list comprehension:

[i for i in a if not any(x in i for x in b)]

This returns:

['blah', 'tete', 'head']

Upvotes: 5

Related Questions