Hello I am new to Python and need some help on this
I have the following list of string
my_list = ["-Public-", "-Private-", "-Protected-"]
and another list
sgList = [ 'launch-wizard-16', 'launch-Private-1', 'default', 'EP-Protected-12', 'default', 'launch-Public-13']
I wanted to check if all the three elements from the my_list list are present in sgList if yes then return True otherwise False. If even one of them is not present then it should return false.
I have the following code but this will return True even if one of them is found.
imported = (any(rails.lower() in sg.lower() for rails in my_list for sg in sgList))
Can someone please guide me on what I can do here to check the presence of all three elements
CodePudding user response:
You can combine all and any:
my_list = ["-Public-", "-Private-", "-Protected-"]
sgList = ['launch-wizard-16', 'launch-Private-1', 'default', 'EP-Protected-12', 'default', 'launch-Public-13']
output = all(any(x in sg for sg in sgList) for x in my_list)
print(output) # True
Given x == '-Public-', for example, any(x in sg for sg in sgList) tests if '-Public-' is a substring of any of the items in sgList. Since there is such an item ('launch-Public-13'), any(x in sg for sg in sgList) evaluates to True.
Now all(... for x in my_list) checks if this holds for every item x in my_list.
CodePudding user response:
all(elem in sgList for elem in my_list)
