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

 `# Python program to fetch the indices` `# of true values in a Boolean list` `from` `itertools ``import` `compress ` `  `  `# initializing list  ` `bool_list ``=` `[``False``, ``True``, ``False``, ``True``, ``True``, ``True``] ` `  `  `# printing given list  ` `print` `("Given ``list` `is` `: " ``+`  `str``(bool_list)) ` `  `  `# using itertools.compress() ` `# to return true indices. ` `res ``=` `list``(compress(``range``(``len``(bool_list )), bool_list )) ` `  `  `# printing result ` `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

 `# Python program to fetch the indices` `# of true values in a Boolean list`   `# initializing list  ` `bool_list ``=` `[``False``, ``True``, ``False``, ``True``, ``True``, ``True``] ` `  `  `# printing given list  ` `print` `("Given ``list` `is` `: " ``+`  `str``(bool_list)) ` `  `  `# using enumerate() + list comprehension ` `# to return true indices. ` `res ``=` `[i ``for` `i, val ``in` `enumerate``(bool_list) ``if` `val] `   `  `  `# printing result ` `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

 `# Python program to fetch the indices` `# of true values in a Boolean list`   `# initializing list  ` `bool_list ``=` `[``False``, ``True``, ``False``, ``True``, ``True``, ``True``] ` `  `  `# printing given list  ` `print` `("Given ``list` `is` `: " ``+`  `str``(bool_list)) ` `  `  `# using lambda + filter() + range() ` `# to return true indices. ` `res ``=` `list``(``filter``(``lambda` `i: bool_list[i], ``range``(``len``(bool_list)))) ` `  `  `# printing result ` `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:  ``# base case: empty list` `        ``return` `[]` `    ``if` `bool_list[``0``]:  ``# if first element is True` `        ``return` `[index] ``+` `true_indices(bool_list[``1``:], index``+``1``)  ``# append index and recurse on rest of list` `    ``else``:  ``# if first element is False` `        ``return` `true_indices(bool_list[``1``:], index``+``1``)  ``# recurse on rest of list`     `# initializing list  ` `bool_list ``=` `[``False``, ``True``, ``False``, ``True``, ``True``, ``True``] ` `  `  `# printing given list  ` `print``(``"Given list is:"``, bool_list)`   `# printing result `   `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`   `# initializing list` `bool_list ``=` `[``False``, ``True``, ``False``, ``True``, ``True``, ``True``]`   `# printing given list` `print``(``"Given list is:"``, bool_list)`   `# converting to numpy array` `bool_arr ``=` `np.array(bool_list)`   `# getting indices of True values` `res ``=` `list``(np.where(bool_arr)[``0``])`   `# printing result` `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.