Search code examples
pythontypeerror

How to solve TypeError: 'bool' object is not iterable in Python


This is my function which throws this error

   TypeError: 'bool' object is not iterable

in line:

   if all(v == 0):

My goal is in this line to check whether all values are equal to Zero.

Here is my method:

def main():


  def checklist( thelist ):
    if len(thelist) > 2:
        for v in thelist: 
            if v < 0.0: 
                print ("One negative")
            if all(v == 0):
                print( "All zero")
            else:
                print("All good")  

alist = [0.0, 0.1, 0.3, 0.0]
checklist( alist ) 




if __name__ == '__main__':
 
# Calling main() function
    main()

What I do not understand is what am I actually checking with this line as I'm apparently not checking my list.

Edited code:

  def checklist( thelist ):
    if len(thelist) > 2:
        for v in thelist: 
            if v < 0.0: 
                print ("One negative")
        if all(vs == 0 for vs in thelist):
            print( "All zero")
        else:
            print("All good")  

alist = [0.0, -0.1, 0.3, 0.0]
checklist( alist ) 

Solution

  • def main():
    
    
      def checklist( thelist ):
        if len(thelist) > 2:
            if all(vs == 0.0 for vs in thelist):
                print( "All zero")
            if any(v < 0.0 for v in thelist): 
                print ("At least one negative")
            else:
                print("All good")  
    
    alist = [-1.0, 1.0, 1.0, 1.0]
    checklist( alist ) 
    

    Solved it! Thanks to @luk2302