-1
mylist = ["my", "try","some","words"]
message = "Do u want my chips?"
if any(x in message for x in mylist):
  print("any of words is in the list")
else:
  print("words not in the message")

this code works fine and I use a similar code in my project but however I want to print the "x" in this code any of words is in the list but which one I want to learn this how do I that

1 Answers1

0

You're looking for something like the "first" equivalent which I don't believe exists natively.

Here is sample code which could give you what you're looking for (from https://github.com/hynek/first/blob/main/first.py):

def first(iterable, default=None, key=None):
    if key is None:
        for el in iterable:
            if el:
                return el
    else:
        for el in iterable:
            if key(el):
                return el
    return default
user15716642
  • 171
  • 1
  • 12