let's say we have a list
a=["a1", "a2", "a3"]
then we have an input:
x=input("enter something: ")
how would you check if that input "x" is equal to any item in the list and then return true?
let's say we have a list
a=["a1", "a2", "a3"]
then we have an input:
x=input("enter something: ")
how would you check if that input "x" is equal to any item in the list and then return true?
You can check it with that code:
print("Yes, x in list" if x in a else "No, x is not in list")
It same that:
if x in a:
print("Yes, x in list")
else:
print("No, x is not in list")
Also if you want to get index of contained element - just use .index()
:
if x in a:
print('Yes, x in list, and it index is:', a.index(x))
In that example i'am avoid else:
construct, but you can use it like in previous example, if you need it.