Home > OS >  Adding more than one parameter of search in a loop
Adding more than one parameter of search in a loop

Time:10-11

I want to add more than one parameter to search in a list of sublist. For example, this is when i am looking just for one specific word.

y2 = [x for x in y2 if 'Entity' in x]

but im looking for a group of words and i just try to put the list in the code, but doesnt work. The error that appears is this.

'in ' requires string as left operand, not list

entities = [["Entity","Entity with some","Entity with audition"]]
y2 = [x for x in y2 if entities  in x]

Thank you for the help.

CodePudding user response:

You can use any.

entities = ["Entity","Entity with some","Entity with audition"]
y2 = [x 
      for x in y2 
      if any(ent in x for ent in entities)
     ]

CodePudding user response:

You could make your own function and use it in the list comprehension.

def f(x, entities):
    for e in entities:
        if e in x:
            return True
    return False

y2 = [x for x in y2 if f(x, entities)]
  • Related