Boolean and Fancy Indexing

Python · NumPy Library

Theory

A comparison on an array returns a boolean array of the same shape: marks >= 60 gives [False, True, True, ...]. Passing that mask back in, marks[marks >= 60], keeps only the positions that are True. Combine conditions with & (and), | (or) and ~ (not), wrapping each one in parentheses: marks[(marks >= 60) & (marks < 90)]. Python's and/or keywords do not work on arrays.

Fancy indexing passes a list of positions instead: marks[[0, 3, 5]] returns those three values in that order, and repeats are allowed. Both styles also work for assignment, so marks[marks < 40] = 40 raises every failing mark to 40 in place. Unlike slices, boolean and fancy indexing return a copy, and on a 2-D array a boolean mask of the same shape returns a flat 1-D result.

Why we use it

Filtering by a condition is the most common data task: passing students, orders above a value, readings out of range. Boolean masks do it for the whole array at once and are the same idea pandas uses for df[df['col'] > x].

When to use it

Use a mask when the selection depends on the values, fancy indexing when you have a list of positions (for example from argsort). Use np.where(cond, a, b) instead when you want to keep every element and choose between two values.

How it works

  1. Array of values
  2. Write a condition: marks >= 60
  3. NumPy returns a True/False mask of the same shape
  4. More than one condition? Yes: Combine with & | ~ and parentheses. No: Use the single mask.
  5. arr[mask] keeps only the True positions
  6. Filtered copy (or arr[mask] = x to edit)

Syntax

mask = arr > value
arr[mask]
arr[(cond1) & (cond2)]  arr[(c1) | (c2)]  arr[~cond]
arr[[i, j, k]]
arr[mask] = new_value

Example

marks = np.array([45, 82, 67, 91])
print(marks[marks >= 60])   # [82 67 91]
print(marks[[0, 3]])         # [45 91]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic