1

I need to remove from my_list anything that contains any substring from banned (both lists are very long, approximately 30k+).

Here's a mini example:

my_list = ['dnb','distinctive_group_inc','real-estate-profile','estate.com']
banned = ['distinctive','estate']

Desired outcome:

my_list = ['dnb']

I'd like to be able to do it in one go so I tried:

my_list = [i for i in banned if i not in my_list]

Since that didn't work at all, I tried it in two steps:

for i in reversed(my_list):
    for j in banned:
        if j in i:
            my_list.remove(i)
        else:
            pass

How can I do it in one step?

Ellie Biessek
  • 148
  • 2
  • 14

1 Answers1

2

If you like list comprehension (and I do), then you were on the right track. This takes your starting comprehension but changes the criteria slightly to to exclude words that have any partial match in the banned list.

my_list = ['dnb','distinctive_group_inc','real-estate-profile','estate.com']
banned = ['distinctive','estate']

my_new_list = [
    word for word in my_list
    if not any(banned_word in word for banned_word in banned)
]

print(my_new_list)

Should result in:

['dnb']
JonSG
  • 10,542
  • 2
  • 25
  • 36