Python program to fetch the indices of true values in a Boolean list
Last Updated :
10 May, 2023
Given a list of only boolean values, write a Python program to fetch all the indices with True values from given list. Let’s see certain ways to do this task.
Method #1: Using itertools [Pythonic way] itertools.compress() function checks for all the elements in list and returns the list of indices with True values.
Python3
from itertools import compress
bool_list = [ False , True , False , True , True , True ]
print ("Given list is : " + str (bool_list))
res = list (compress( range ( len (bool_list )), bool_list ))
print ("Indices having True values are : " + str (res))
|
Output:
Given list is : [False, True, False, True, True, True]
Indices having True values are : [1, 3, 4, 5]
Time complexity: O(n), where n is the number of elements in the boolean list.
Space complexity: O(m), where m is the number of true values in the boolean list.
Method #2: Using enumerate() method enumerate() method hashes the index with its value and coupled with list comprehension can let us check for the true values.
Python3
bool_list = [ False , True , False , True , True , True ]
print ("Given list is : " + str (bool_list))
res = [i for i, val in enumerate (bool_list) if val]
print ("Indices having True values are : " + str (res))
|
Output:
Given list is : [False, True, False, True, True, True]
Indices having True values are : [1, 3, 4, 5]
Time Complexity : O(n)
Space Complexity : O(n)
Method #3: Using filter() + range()
Python3
bool_list = [ False , True , False , True , True , True ]
print ("Given list is : " + str (bool_list))
res = list ( filter ( lambda i: bool_list[i], range ( len (bool_list))))
print ("Indices having True values are : " + str (res))
|
Output:
Given list is : [False, True, False, True, True, True]
Indices having True values are : [1, 3, 4, 5]
Time Complexity : O(n)
Space Complexity : O(n)
Method#4: Using Recursive method.
Algorithm:
- The function takes a boolean list as input and an optional index parameter (which is initially set to 0).
- It checks the base case, i.e., if the boolean list is empty, it returns an empty list.
- It then checks the first element of the boolean list. If it’s True, it appends the current index to the result and recursively
- calls the function with the rest of the list and the index incremented by 1.
- If the first element is False, it simply calls the function recursively with the rest of the list and the index incremented by 1.
- Finally, it returns the list of indices that have True values.
Python3
def true_indices(bool_list, index = 0 ):
if not bool_list:
return []
if bool_list[ 0 ]:
return [index] + true_indices(bool_list[ 1 :], index + 1 )
else :
return true_indices(bool_list[ 1 :], index + 1 )
bool_list = [ False , True , False , True , True , True ]
print ( "Given list is:" , bool_list)
print ( "Indices having True values are:" , true_indices(bool_list))
|
Output
Given list is: [False, True, False, True, True, True]
Indices having True values are: [1, 3, 4, 5]
Time complexity:
In the worst case, the function needs to examine every element of the boolean list.
For each element, it either appends the index to the result or skips to the next element, so the time complexity is O(N) where N is the length of the boolean list.
Space complexity:
The function uses recursion to process the list and create a list of indices that have True values.
In the worst case, when all elements of the boolean list are True, the function creates a list of size N, where N is the length of the boolean list.
Therefore, the space complexity is O(N) where N is the length of the boolean list.
Numpy approach to fetch the indices of True values in a boolean list:
Algorithm:
Convert the boolean list to a numpy array.
Use np.where() function to get the indices of True values.
Convert the resulting tuple to a list.
Python3
import numpy as np
bool_list = [ False , True , False , True , True , True ]
print ( "Given list is:" , bool_list)
bool_arr = np.array(bool_list)
res = list (np.where(bool_arr)[ 0 ])
print ( "Indices having True values are:" , res)
|
Output:
Given list is: [False, True, False, True, True, True]
Indices having True values are: [1, 3, 4, 5]
Time complexity: O(n), where n is the number of elements in the boolean list.
Space complexity: O(n), where n is the number of elements in the boolean list.
Share your thoughts in the comments
Please Login to comment...