Sometimes, we need to check if a list is completely True of False, these occurrences come more often in testing purposes after the development phase. Hence, having a knowledge of all this is necessary and useful. Lets discuss certain ways in which this can be performed.
Method #1: Naive Method
In the naive method, we just run a loop from beg to end of list and check manually for each value. This is the most basic way to perform this particular task.
Python3
# Python3 code to demonstrate # to check for False list # using naive method # initializing list test_list = [ True , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) flag = 0 # using naive method # to check for False list for i in test_list: if i = = True : flag = 1 break # printing result print ( "Is List completely false ? : " + str ( bool ( not flag))) |
The original list is : [True, False, False, False] Is List completely false ? : False
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #2: Using all()
This function tests each value to be False and if yes, returns boolean True, else returns false. The list iteration is done using list comprehension.
Python3
# Python3 code to demonstrate # to check for False list # using all() # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) flag = 0 # using all() # to check for False list res = all ( not i for i in test_list) # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [False, False, False, False] Is List completely false ? : True
Time Complexity: O(n), where n is the length of the input list. This is because we’re using all() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #3: Using any()
This function tests for any one of the True value, if found returns True, else returns False value. Negation of this function is used as the result.
Python3
# Python3 code to demonstrate # to check for False list # using any() # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) # using any() # to check for False list res = not any (test_list) # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [False, False, False, False] Is List completely false ? : True
Method #4 : Using count() and len() method
Python3
# Python3 code to demonstrate # to check for False list # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) res = False # to check for False list if (test_list.count( False ) = = len (test_list)): res = True # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [False, False, False, False] Is List completely false ? : True
Method #5 : Using * operator and len() method
Python3
# Python3 code to demonstrate # to check for False list # initializing list test_list = [ True , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) res = False x = [ False ] * len (test_list) if (x = = test_list): res = True # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [True, False, False, False] Is List completely false ? : False
Method #6 : using filter(),lambda functions
Python3
# Python3 code to demonstrate # to check for False list # using filter(),lambda functions # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) flag = 0 res = True # to check for False list resultList = list ( filter ( lambda x: x ! = False , test_list)) if (resultList): res = False # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [False, False, False, False] Is List completely false ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using built-in index() method:
Here is an example of how you can use the index method to check if a list is completely False:
Python3
test_list = [ False , False , False , False ] print ( "The original list is:" , test_list) result = False try : index = test_list.index( True ) except ValueError: result = True print ( "Is List completely false? :" , result) #This code is contributed by Edula Vinay Kumar Reddy |
The original list is: [False, False, False, False] Is List completely false? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method #8 : Using operator.countOf() method
Python3
# Python3 code to demonstrate # to check for False list import operator as op # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) res = False # to check for False list if (op.countOf(test_list, False ) = = len (test_list)): res = True # printing result print ( "Is List completely false ? : " + str (res)) |
The original list is : [False, False, False, False] Is List completely false ? : True
Time complexity: O(n)
Auxiliary Space: O(1)
Method #9:Using recursion
Algorithm:
- Define a function is_list_false that takes a list test_list as an argument.
- If the length of the list is 0, return True, since an empty list is considered completely false.
- If the first element of the list is True, return False, since the list is not completely false.
- Otherwise, recursively call the is_list_false function with the remaining elements of the list (i.e., test_list[1:]).
- Repeat steps 2-4 until the length of the list is 0 or the first element of the list is True.
- If the length of the list is 0, return True, since all the elements of the list were False.
Python3
def is_list_false(test_list): if len (test_list) = = 0 : return True elif test_list[ 0 ] = = True : return False else : return is_list_false(test_list[ 1 :]) # initializing list test_list = [ False , False , False , False ] # printing original list print ( "The original list is : " + str (test_list)) # checking if list is completely false using recursion print ( "Is List completely false ? : " + str (is_list_false(test_list))) #This code is contributed by Vinay Pinjala. |
The original list is : [False, False, False, False] Is List completely false ? : True
Time Complexity:
In the worst case scenario, where the list contains all False values, the function will traverse through the entire list until the length of the list becomes 0. Therefore, the time complexity of the function is O(n), where n is the length of the list.
Auxiliary Space:
The auxiliary space of the function is O(n), where n is the length of the list. This is because the function uses recursion, which creates a new stack frame for each recursive call. Therefore, in the worst case scenario, where the list contains n elements, the function will create n stack frames, each with its own set of local variables. However, once the recursion is complete, the stack frames are popped off the stack, so the overall space used by the function is limited to O(n).
Method #10: Using reduce method.
Algorithm:
1. `reduce()` function takes a function (usually a lambda function), a sequence (list or tuple), and an initial value.
2. The function is applied to the first two elements of the sequence and the result is returned.
3. The function is applied to the result and the next element in the sequence, and the result is returned.
4. This process is repeated until the entire sequence is processed.
5. The final result is returned
Python3
from functools import reduce test_list = [ True , False , False , False ] # using reduce() method to check if all elements are False result = reduce ( lambda x, y: x and not y, test_list, True ) # printing result print ( "Is List completely false ? : " + str (result)) |
Is List completely false ? : False
Time complexity:
The time complexity of the `reduce()` method is O(N), where N is the length of the input list. This is because `reduce()` applies the lambda function to each element of the input list once.
Space Complexity:
The space complexity of the `reduce()` method is also O(1), because it only needs to store the result of the lambda function and the next element of the list at any given time. This means that the space used by the `reduce()` function is constant and does not depend on the length of the input list.