Python | True Record
Last Updated :
28 Feb, 2023
Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values. This kind of problem is common in data preprocessing steps. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using not + any() + map() + lambda
Combination of above functions can be used to perform this task. In this, we check for any element using any() and extension of logic is done by map() and lambda.
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = not any ( map ( lambda ele: not ele, test_tup))
print ( "Is Tuple True ? : " + str (res))
|
Output :
The original tuple : (True, True, True, True)
Is Tuple True ? : True
Method #2: Using all() This checks for truthness of all elements of tuple using all(), returns True if there is no False element.
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = all (test_tup)
print ( "Is Tuple True ? : " + str (res))
|
Output :
The original tuple : (True, True, True, True)
Is Tuple True ? : True
Method #3 : Using count() and len() methods
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = False
if (test_tup.count( True ) = = len (test_tup)):
res = True
print ( "Is Tuple True ? : " + str (res))
|
Output
The original tuple : (True, True, True, True)
Is Tuple True ? : True
Method #4 : Using * and tuple(),len() methods
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = False
x = [ True ] * len (test_tup)
x = tuple (x)
if (x = = test_tup):
res = True
print ( "Is Tuple True ? : " + str (res))
|
Output
The original tuple : (True, True, True, True)
Is Tuple True ? : True
Method #5: Using set() and len() methods
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = False
if len ( set (test_tup)) = = 1 and True in set (test_tup):
res = True
print ( "Is Tuple True ? : " + str (res))
|
Output
The original tuple : (True, True, True, True)
Is Tuple True ? : True
This method uses the set() function to convert the tuple into a set, then checks if the length of the set is 1 and that the only element in the set is True.
Time complexity: O(n)
Space complexity: O(n)
Method #6 : Using operator.countOf(),len() methods
Python3
test_tup = ( True , True , True , True )
print ( "The original tuple : " + str (test_tup))
res = False
import operator
if (operator.countOf(test_tup, True ) = = len (test_tup)):
res = True
print ( "Is Tuple True ? : " + str (res))
|
Output
The original tuple : (True, True, True, True)
Is Tuple True ? : True
Time complexity: O(n)
Auxiliary Space: O(n)
Method#7: Using the sum() function
Step-by-step algorithm for implementing the approach
- Initialize the tuple.
- Compute the sum of the tuple using the sum() function.
- Compute the length of the tuple using the len() function.
- Check if the sum of the tuple is equal to the length of the tuple.
- If the sum of the tuple is equal to the length of the tuple, all elements of the tuple are True. Otherwise, at least one element of the tuple is not True.
Python3
test_tup = ( True , True , True , True )
res = sum (test_tup) = = len (test_tup)
print ( "Is Tuple True ? : " + str (res))
|
Output
Is Tuple True ? : True
The time complexity of this approach is O(n), where n is the length of the tuple. The sum() function and the len() function both have a time complexity of O(n), so the overall time complexity is O(n).
The auxiliary space complexity of this approach is O(1), because it only requires a constant amount of extra space to store the sum and the length of the tuple.
This approach is simple and easy to understand, but it requires iterating over the entire tuple twice, which could be slower than some other approaches. However, for small tuples, the difference in performance is likely to be negligible.
Method#8: Using numpy.all() function from numpy module:
1.Import the numpy module.
2.Initialize a tuple containing boolean values.
3.Call the np.all() function from the numpy module, passing in the tuple as an argument.
4.Store the result in a variable named all_true.
5.Print the value of all_true.
Python3
import numpy as np
test_tup = ( True , True , True , True )
all_true = np. all (test_tup)
print ( "Is Tuple True? : " + str (all_true))
|
Output:
Is Tuple True? : True
The time complexity : O(n), where n is the number of elements in the tuple. This is because the function needs to check each element in the tuple to determine if it is True or False.
The auxiliary space : O(1), since it only uses a constant amount of additional memory to store the tuple and the all_true variable. The np.all() function does not use any additional memory, since it operates on the input tuple in place.
Share your thoughts in the comments
Please Login to comment...