Python program to fetch the indices of true values in a Boolean list
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.
# 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]
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.
# 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]
Method #3: Using filter()
+ range()
# 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]