I have the following code:
MFII_files = ['patrick', 'domccc', 'joseph', 'harry']
file_name = 'ccc'
if file_name in MFII_file for MFII_file in MFII_files:
print('yes')
But when I run it, it says invalid syntax pointing to the for
. Why does this not work?
Use any
to perform this check instead.
if any(file_name in MFII_file for MFII_file in MFII_files):
any
takes an iterable and returns a result indicating whether any element of the iterable was true. In this case, we can conveniently pass a generator expression.