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
Answer from Alexander L. Hayes on Stack OverflowVideos
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
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)
Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module.
For example, the following code in a foo.py explicitly exports the symbols bar and baz:
__all__ = ['bar', 'baz']
waz = 5
bar = 10
def baz(): return 'baz'
These symbols can then be imported like so:
from foo import *
print(bar)
print(baz)
# The following will trigger an exception, as "waz" is not exported by the module
print(waz)
If the __all__ above is commented out, this code will then execute to completion, as the default behaviour of import * is to import all symbols that do not begin with an underscore, from the given namespace.
Reference: https://docs.python.org/tutorial/modules.html#importing-from-a-package
NOTE: __all__ affects the from <module> import * behavior only. Members that are not mentioned in __all__ are still accessible from outside the module and can be imported with from <module> import <member>.
It's a list of public objects of that module, as interpreted by import *. It overrides the default of hiding everything that begins with an underscore.