Reputation: 159
I'm trying to run some function if a list of booleans is true. My list consists of boolean functions.
list = [file.endswith('05',21,22), file.endswith('11',21,22),
file.endswith('17',21,22), file.endswith('21',21,22),
file.endswith('23',21,22)]
if any(True in list) == True:
# do something
Currently, the if clause gives me an error
if any(True in list) == True:
TypeError: 'bool' object is not iterable
Not really sure how to fix it.
Upvotes: 2
Views: 4886
Reputation: 74645
Note that your code is much shorter if written as:
if any(file.endswith(suffix, 21, 22)
for suffix in ['05', '11', '17', '21', '23']):
Upvotes: 2
Reputation: 19733
any(iterable):
Return True if any element of the iterable is true. If the iterable is empty, return False.
Your code should use:
if any(list):
Upvotes: 1
Reputation:
any
is expecting an iterable argument, which it will then run through to see if any of its items evaluate to True
. Moreover, the expression True in list
returns a non-iterable boolean value:
>>> lst = [False, True, False]
>>> True in lst
True
>>>
To fix the problem, you should simply pass the list to any
:
if any(list):
You should also refrain from making a user-defined name the same as one of the built-ins. Doing so overshadows the built-in and makes it unusable in the current scope.
Upvotes: 4