Skip to content
Advertisement

Get items from list if the item contains any of the strings in another list (Python) [closed]

I have a list that looks like:

JavaScript

I would like all items in the list if it contains ab or ac. From the example above, I would like the list to be:

JavaScript

I have tried the following, but it gives me a TypeError.

JavaScript

Advertisement

Answer

You’re almost there but you can’t cross check multiple substrings against a string. What you can do is use two conditions with an or:

JavaScript

If you have a list of substrings to check, you can use any() in your condition but you’ll have to iterate over the substrings:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement