Open In App

Python – Test if a list is completely True

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Pure List Test
# using naive method
 
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print ("The original list is : " + str(test_list))
 
flag = 0
 
# using naive method
# Pure List Test
for i in test_list :
    if not i :
        flag = 1
        break
 
# printing result
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




# Python3 code to demonstrate
# Pure List Test
# using all()
 
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print ("The original list is : " + str(test_list))
 
flag = 0
 
# using all()
# Pure List Test
res = all(i for i in test_list)
 
# printing result
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




# Python3 code to Test if a list is completely True
 
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = False
if(test_list.count(True) == len(test_list)):
    res = True
 
# printing result
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




# Python3 code to Test if a list is completely True
 
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = False
x=[True]*len(test_list)
if(x==test_list):
    res=True
# printing result
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




# Python3 code to demonstrate
# Pure List Test
# using Counter() function
from collections import Counter
# initializing list
test_list = [True, True, True, True, False]
 
# printing original list
print("The original list is : " + str(test_list))
 
flag = False
 
freq = Counter(test_list)
 
if(freq[True] == len(test_list)):
    flag = True
# printing result
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




# Python3 code to Test if a list is completely True
import operator as op
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = False
if(op.countOf(test_list,True) == len(test_list)):
    res = True
 
# printing result
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))
 
# Using the reduce function to apply the and_ operator to all elements in the list
res = reduce(and_, test_list)
 
# print result
print("Is List completely True ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Python3 code to demonstrate
# Pure List Test
# using recursion
def check(l,i):
  if len(l) == i:
    return True
  if l[i] == False:
    return False
  return check(l,i+1)
 
# initializing list
test_list = [True, True, True, True]
 
# printing original list
print ("The original list is : " + str(test_list))
 
flag = check(test_list,0)
 
# printing result
print ("Is List completely True ? : ",flag)
#This code is contributed by vinay Pinjala.


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:

  1. Import the numpy library.
  2. Pass the input iterable as an argument to the np.all() function.
  3. Iterate over the elements of the iterable.
  4. If any element is false, return False.
  5. If all elements are true, return True.

Python3




import numpy as np
# initializing list
test_list = [True, True, True, True]
# printing original list
print ("The original list is : " + str(test_list))
  
res = np.all(test_list)
 
print("Is List completely True ? : " + str(res))
#This code is contributed by Jyothi pinjala


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads