Python | Get indices of True values in a binary list
Boolean lists are often used by the developers to check for True values during hashing. Boolean list is also used in certain dynamic programming paradigms in dynamic programming. Let’s discuss certain ways to get indices of true values in list in Python.
Method #1 : Using enumerate() and list comprehension
enumerate() can do the task of hashing index with its value and coupled with list comprehension can let us check for the true values.
# Python3 code to demonstrate # to return true value indices. # using enumerate() + list comprehension # initializing list test_list = [True, False, True, False, True, True, False] # printing original list print ("The original list is : " + str(test_list)) # using enumerate() + list comprehension # to return true indices. res = [i for i, val in enumerate(test_list) if val] # printing result print ("The list indices having True values are : " + str(res)) |
The original list is : [True, False, True, False, True, True, False] The list indices having True values are : [0, 2, 4, 5]
Method #2 : Using lambda + filter() + range()
filter() function coupled with lambda can perform this task with help of range function. range() function is used to traverse the entire list and filter checks for true values.
# Python3 code to demonstrate # to return true value indices. # using lambda + filter() + range() # initializing list test_list = [True, False, True, False, True, True, False] # printing original list print ("The original list is : " + str(test_list)) # using lambda + filter() + range() # to return true indices. res = list(filter(lambda i: test_list[i], range(len(test_list)))) # printing result print ("The list indices having True values are : " + str(res)) |
The original list is : [True, False, True, False, True, True, False] The list indices having True values are : [0, 2, 4, 5]
Method #3 : Using itertools.compress()
compress function checks for all the elements in list and returns the list of indices with True values. This is most Pythonic and elegant way to perform this particular task.
# Python3 code to demonstrate # to return true value indices. # using itertools.compress() from itertools import compress # initializing list test_list = [True, False, True, False, True, True, False] # printing original list print ("The original list is : " + str(test_list)) # using itertools.compress() # to return true indices. res = list(compress(range(len(test_list)), test_list)) # printing result print ("The list indices having True values are : " + str(res)) |
The original list is : [True, False, True, False, True, True, False] The list indices having True values are : [0, 2, 4, 5]
Recommended Posts:
- Python program to fetch the indices of true values in a Boolean list
- Python | Find indices with None values in given list
- Python | Segregate True and False value indices
- Python | Count true booleans in a list
- Python | Test if any list element returns true for condition
- Python | Indices of sorted list of list elements
- Python | Indices list of matching element from other list
- Python | Find elements of a list by indices
- Python | Duplicate element indices in list
- Python | Indices of N largest elements in list
- Python | Ways to find indices of value in list
- Python | Group elements at same indices in a multi-list
- Python | Remove all values from a list present in other list
- Python | Sort the values of first list using second list
- Python | Remove None values from list
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.



