-1

I have a code that checks if a string is in a list of strings like,

    for a in array:
        if x in a:
            return True
    return False

if a string is found in the list, return True and False otherwise. I am wondering if it can be written in one line of code.

daiyue
  • 7,196
  • 25
  • 82
  • 149

1 Answers1

3

You can use any to iterate through a sequence and see if any item matches some particular condition.

return any(x in a for a in array)
khelwood
  • 55,782
  • 14
  • 81
  • 108