Get indices of True values in a boolean List in Python

In this article, we will discuss different ways to get the indices of True values in a bool list in Python.

Table Of Contents

Introduction

Suppose we have a list that contains only bool values,

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

Now we want to find the indices of all all True values in this List. Like for the above list, the indices of True values will be,

[0, 3, 7, 8, 9]

There are different ways to do it. Let’s discuss them one by one.

Advertisements

Method 1: Using List comprehension

The enumerate() function accepts a list as an argument, and for each element of list, it yields a pair containing the index position and the value. We can pass our list in the enumerate() function, and call it inside the list comprehension. It will filter only True values, but will store only index positions in the new list. Let’s see an example,

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

# Get indices of True values in a list
indices = [ index for index, value in enumerate(listOfBoolValues) if value]

print(indices)

Output

[0, 3, 7, 8, 9]

Here, we iterated over all the elements of list, by the value and index position. Then we filtered only True values, and stored the corresponding indices in a new list, using the List comprehension.

Method 2: Using for loop & enumerate()

Pass the list in the enumerate() function, and for each list element, it will yields a pair containing the index position and the value. Loop through these pairs, and look for True values only. For each True value, store the corresponding index position in the new list. Let’s see an example,

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

indices = []

# Iterate over all values, along with the index position
for index, value in enumerate(listOfBoolValues):
    # For True value, Store the index poistion in list
    if value:
        indices.append(index)

print(indices)

output

[0, 3, 7, 8, 9]

It gave the indices of True values in the boolean list.

Method 3: Using NumPy

The numpy module provides a function where(), to filter the True and False Values in a sequence. Pass the list of bool values in the where() function. It will return an array containing two subarrays. First SubArray will have indices of all True values in the given sequence. Second SubArray will be empty in our case. Therefore, convert the first subarray to a list using tolist() function. Let’s see an example,

import numpy as np

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

# Get indices of True values in a list
indices = np.where(listOfBoolValues)[0].tolist()

print(indices)

output

[0, 3, 7, 8, 9]

It gave the indices of True values in the boolean list.

Method 4: Using filter() function

Create a sequence of numbers from 0 till LEN, where LEN is the size of list. Pass this sequence to the filter() function, along with a lambda function. Inside the lambda function, for the given index, check if the value in list is True or not. The filter() function will filter only those numbers from the sequence, for which lambda function returns True. This way we will get the indices of True values in a boolean list. Let’s see an example,

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

# Get indices of True values in a list
indices = list(filter(lambda index: listOfBoolValues[index], range(len(listOfBoolValues))))

print(indices)

output

[0, 3, 7, 8, 9]

It gave the indices of True values in the boolean list.

Method 5: Using itertools

The compress() function in itertools accepts a range containing the index positions of sequence and the sequence itself as arguments. It returns the indices of True values in the sequence. Let’s see an example,

from itertools import compress

listOfBoolValues = [True, False, False, True, False, False, False, True, True, True, False]

# Get indices of True values in a list
indices = list(compress( range(len(listOfBoolValues)), listOfBoolValues))

print(indices)

output

[0, 3, 7, 8, 9]

It gave the indices of True values in the boolean list.

Summary

We learned about five different ways to get the indices of True values in a List in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top