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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output:

Given list is : [False, True, False, True, True, True]
Indices having True values are : [1, 3, 4, 5]

 

Method #3: Using filter() + range()

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output:

Given list is : [False, True, False, True, True, True]
Indices having True values are : [1, 3, 4, 5]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.