Python – Test if a list is completely True
Sometimes, we need to check if a list is completely True, 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
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
flag = 0
for i in test_list :
if not i :
flag = 1
break
print ( "Is List completely True ? : " + str ( bool ( not flag)))
|
Output :
The original list is : [True, True, True, True]
Is List completely True ? : True
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using all() This function tests each value to be True and if yes, returns boolean True, else returns false. The list iteration is done using list comprehension.
Python3
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
flag = 0
res = all (i for i in test_list)
print ( "Is List completely True ? : " + str (res))
|
Output :
The original list is : [True, True, True, True]
Is List completely True ? : 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), constant extra space is required
Method #3 : Using count() and len() methods
Python3
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
res = False
if (test_list.count( True ) = = len (test_list)):
res = True
print ( "Is List completely True ? : " + str (res))
|
Output
The original list is : [True, True, True, True]
Is List completely True ? : True
Time complexity: O(n), where n is the length of the numbers list.
Auxiliary Space: O(n),where n is the length of the numbers list.
Method #4 : Using * operator and len() method
Python3
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
res = False
x = [ True ] * len (test_list)
if (x = = test_list):
res = True
print ( "Is List completely True ? : " + str (res))
|
Output
The original list is : [True, True, True, True]
Is List completely True ? : True
Method #5 : Using Counter() function
Python3
from collections import Counter
test_list = [ True , True , True , True , False ]
print ( "The original list is : " + str (test_list))
flag = False
freq = Counter(test_list)
if (freq[ True ] = = len (test_list)):
flag = True
print ( "Is List completely True ? : " + str (flag))
|
Output
The original list is : [True, True, True, True, False]
Is List completely True ? : False
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 6: using operator.countOf() method
Python3
import operator as op
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
res = False
if (op.countOf(test_list, True ) = = len (test_list)):
res = True
print ( "Is List completely True ? : " + str (res))
|
Output
The original list is : [True, True, True, True]
Is List completely True ? : True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method 7: Using the reduce() function from the functools module:
Python3
from functools import reduce
from operator import and_
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
res = reduce (and_, test_list)
print ( "Is List completely True ? : " + str (res))
|
Output
The original list is : [True, True, True, True]
Is List completely True ? : True
This approach uses the reduce() function to apply the and_ operator to all elements in the list. The and_ operator returns True only if all operands are True, which is used to check if all elements in the list are True.
Time complexity: O(n)
Auxiliary Space: O(1)
Method 8: Using recursion
Python3
def check(l,i):
if len (l) = = i:
return True
if l[i] = = False :
return False
return check(l,i + 1 )
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
flag = check(test_list, 0 )
print ( "Is List completely True ? : " ,flag)
|
Output
The original list is : [True, True, True, True]
Is List completely True ? : True
Time complexity: O(n)
Auxiliary Space: O(n)
Method 9:Using numpy:
- Import the numpy library.
- Pass the input iterable as an argument to the np.all() function.
- Iterate over the elements of the iterable.
- If any element is false, return False.
- If all elements are true, return True.
Python3
import numpy as np
test_list = [ True , True , True , True ]
print ( "The original list is : " + str (test_list))
res = np. all (test_list)
print ( "Is List completely True ? : " + str (res))
|
Output:
The original list is : [True, True, True, True]
Is List completely True ? : True
Time complexity: The np.all() function has a time complexity of O(n), where n is the number of elements in the input iterable.
Space complexity: The space complexity of np.all() is O(1), since it only requires a constant amount of space to store the result. However, it should be noted that the function uses the numpy library, which may require additional memory depending on the size of the input iterable and the system configuration.
Last Updated :
17 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...