I want to check if all elements of a list are not present in a string.
ex :
l = ["abc","ghi"]
s1 = "xyzjkl"
s2 = "abcdef"
So , when l is compared with s1 it should return True, when l is compared with s2 it should return False.
This is what i tried :
all(x for x in l if x not in s1) = True
all(x for x in l if x not in s2) = True
I am getting True for both cases, But it should be false in second case. Can someone please help, any solution will help, i just want to have it in a single line.
Thanks,
CodePudding user response:
You need a list of True, False. But you were simply getting the matched items, so when you do an all
on Truthy values you will get True. Instead do:
all([x not in s1 for x in l])
all([x not in s2 for x in l])
or just without list comp, because all
accepts an iterable.
all(x not in s1 for x in l)
all(x not in s2 for x in l)
CodePudding user response:
If the goal is:
Check that all elements of a list are not present in a string.
The pattern should be: all(s not in my_string for s in input_list)
l = ["abc","ghi"]
s1 = "xyzjkl"
s2 = "abcdef"
print(all(s not in s1 for s in l)) # True
print(all(s not in s2 for s in l)) # False
CodePudding user response:
I think what you want to do is:
all(x not in s1 for x in l) # True
all(x not in s2 for x in l) # False