2

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.

1
  • Don't shadow the builtin list. Commented Feb 10, 2015 at 3:15

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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']):

Comments

1

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):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.