xxxxxxxxxx
# To check if a certain element is contained in a list use 'in'
bikes = ['trek', 'redline', 'giant']
'trek' in bikes
# Output:
# True
xxxxxxxxxx
if any(ext in url_string for ext in extensionsToCheck):
print(url_string)
xxxxxxxxxx
listA = [item1, item2, item3]
if item4 in listA:
print('yes, item4 is in the list')
else:
print('no, item4 is not in the list')
xxxxxxxxxx
if value in list:
#do stuff
#Also to check if it doesn't contain
if value not in list:
#do stuff
xxxxxxxxxx
#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")
xxxxxxxxxx
>>> items = set([-1, 0, 1, 2])
>>> set([1, 2]).issubset(items)
True
>>> set([1, 3]).issubset(items)
False
xxxxxxxxxx
#There's an all() and any() function to do this. To check if big contains ALL elements in small
result = all(elem in big for elem in small)
#To check if small contains ANY elements in big
result = any(elem in big for elem in small)
#the variable result would be boolean (TRUE/FALSE).
xxxxxxxxxx
list = ["a", "a", "a", "b", "c", "d"]
if 'a' in list:
print("a is in the list")