🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.extract()

AI & DATA SCIENCE // np-extract

np.extract() returns the elements of an array that satisfy a given boolean condition, functionally equivalent to boolean indexing but expressed as an explicit function call.

Syntax

np.extract(condition, arr)

Deep Dive Course

np.extract(condition, arr) is functionally identical to indexing the array directly with that same boolean condition — both return a flattened 1D array of the elements where condition is True — but extract() takes the condition and array as two separate function arguments rather than using bracket-indexing syntax. It exists mostly for situations where a function-call style fits better, such as passing it as a callback, or when it slightly improves readability for a particular piece of code, but boolean indexing is the far more common and idiomatic way to express the same operation in everyday NumPy code.

1Understanding np.extract()

np.extract(condition, arr) is functionally identical to indexing the array directly with that same boolean condition — both return a flattened 1D array of the elements where condition is True — but extract() takes the condition and array as two separate function arguments rather than using bracket-indexing syntax. It exists mostly for situations where a function-call style fits better, such as passing it as a callback, or when it slightly improves readability for a particular piece of code, but boolean indexing is the far more common and idiomatic way to express the same operation in everyday NumPy code.

💡

np.extract(condition, arr) and directly indexing the array with that same condition do exactly the same thing — boolean indexing is the far more common, idiomatic way to write it in everyday code, so reach for extract() mainly when its function-call form specifically fits your situation better.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))
localhost:3000

2Practical Example

Here is a real-world application of np.extract() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))
print(arr[arr % 2 == 0])
localhost:3000

3Best Practices

Follow these guidelines when working with np.extract():

1. Prefer boolean indexing over np.extract() in everyday code, since it's the more common, idiomatic NumPy style

2. Reach for np.extract() specifically when a function-call form is more convenient, such as passing it around as a callable

3. Keep the condition and array arguments in the correct order, condition first, then the array, since reversing them silently produces a different, likely broken result

⚠️

Tip: np.extract(condition, arr) and directly indexing the array with that same condition do exactly the same thing — boolean indexing is the far more common, idiomatic way to write it in everyday code, so reach for extract() mainly when its function-call form specifically fits your situation better.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))
Example 02Advanced Example
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))
print(arr[arr % 2 == 0])

Best Practices

  • Prefer boolean indexing over np.extract() in everyday code, since it's the more common, idiomatic NumPy style
  • Reach for np.extract() specifically when a function-call form is more convenient, such as passing it around as a callable
  • Keep the condition and array arguments in the correct order, condition first, then the array, since reversing them silently produces a different, likely broken result

Interview Question

What's the practical difference between np.extract(condition, arr) and boolean indexing with that same condition?

Hint: Think about whether there's any behavioral difference at all, versus just a stylistic one.

There's no behavioral difference — both produce exactly the same flattened array of elements where the condition is True, since extract() is implemented internally using the same boolean-indexing mechanism. The only difference is syntactic: extract() expresses the operation as an explicit function call with the condition and array as separate arguments, while bracket-indexing embeds the condition directly inside the array access. Boolean indexing is by far the more common and idiomatic style in everyday NumPy code, with extract() mainly useful when a function-call form specifically fits better, like passing the operation around as a callable.

Exercises

MediumPractice using np.extract() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
print(np.extract(arr % 2 == 0, arr))

Frequently Asked Questions

What's the practical difference between np.extract(condition, arr) and boolean indexing with that same condition?

There's no behavioral difference — both produce exactly the same flattened array of elements where the condition is True, since extract() is implemented internally using the same boolean-indexing mechanism. The only difference is syntactic: extract() expresses the operation as an explicit function call with the condition and array as separate arguments, while bracket-indexing embeds the condition directly inside the array access. Boolean indexing is by far the more common and idiomatic style in everyday NumPy code, with extract() mainly useful when a function-call form specifically fits better, like passing the operation around as a callable.

Related Functions

boolean-indexingnp-wherenp-argwhere