Python | Test for False list
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 code to demonstrate # to check for False list # using naive method # initializing list test_list = [ False , 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 : [False, False, False, False] Is List completely false ? : True
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 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
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 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
Recommended Posts:
- Python | Remove False row from matrix
- Python | Prefix Sum Subarray till False value
- Python | Segregate True and False value indices
- Python | Test for nested list
- Python | Test if all elements are present in list
- Python | Test list element similarity
- Python | Test if string contains element from list
- Python | Test if any list element returns true for condition
- editable=False - Django Built-in Field Validation
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert mixed data types tuple list to string list
- Python | Find maximum length sub-list in a nested list
- Python | Sorting list of lists with similar list elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.