string = "hello"
list = ["bye", "kasd", "hello", "day", "hel"]
if string in list:
print(f"Found '{string}' in '{list}'!")
else:
print(f"Couldnt find '{string}' in '{list}'!")
>>> Found 'hello' in '["bye", "kasd", "hello", "day", "hel"]'!
# plz suscribe to my youtube channel -->
# https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
fruits = ["apple", "banana", "cherry","banana"]
item = "apple"
if item in fruits:
print("Yes, '",item,"' is in the fruits list")
#This is the list. You can place it in other file and import it.
"In the same file:"
MyList = ["something", "something2", "something3"]
IncredibleWord = "something"
if IncredibleWord in MyList:
print("Yes, IncredibleWord is in your list")
else:
print("No, IncredibleWord isn't in your list")
#------------------------------------------------------------------------------
"If your list is in an other file:"
#OtherFile
MyList = ["something", "something2", "something3"]
#MyMainFile
#Variables and lists for example
from OtherFile import *
IncredibleWord = "something"
if IncredibleWord in MyList:
print("Yes, IncredibleWord is in your list")
else:
print("No, IncredibleWord isn't in your list")
#-------------------------------------------------------------------------------
#Only a variable or a list for example
from OtherFile import <List Name>
IncredibleWord = "something"
if IncredibleWord in MyList:
print("Yes, IncredibleWord is in your list")
else:
print("No, IncredibleWord isn't in your list")
element_you_want_to_find in list_in_you_want_to_search
Note: Don't forget to substitute both of that variables with the variables you want
Conclusion: Use the in operator