numpy
python
boolean array
numpy tutorial
data science

How do I create a numpy array of all True or all False?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In NumPy, an "all true" or "all false" array is just an array with dtype=bool whose values are initialized consistently. The main choice is not whether NumPy can do it, but which constructor best matches your intent: shape-based creation, value-based filling, or copying the shape of another array.

The simplest options

If you know the shape you want, np.ones and np.zeros are the most direct constructors.

python
1import numpy as np
2
3all_true = np.ones((2, 3), dtype=bool)
4all_false = np.zeros((2, 3), dtype=bool)
5
6print(all_true)
7print(all_false)

Output:

python
1[[ True  True  True]
2 [ True  True  True]]
3[[False False False]
4 [False False False]]

This works because NumPy converts numeric 1 to True and numeric 0 to False when the array type is boolean.

np.full is the clearest "fill with one value" API

If you want the code to say exactly what value is being used, np.full is often the most readable option.

python
1import numpy as np
2
3all_true = np.full((4, 2), True, dtype=bool)
4all_false = np.full((4, 2), False, dtype=bool)
5
6print(all_true.dtype)
7print(all_false.dtype)

This is especially nice when the initializer is not numeric, because the intent is obvious: fill the whole array with one repeated value.

For boolean arrays, np.full and np.ones or np.zeros are both fine. Readability is the deciding factor more than performance.

Use the _like variants when shape should match another array

If you already have an array and want a boolean array with the same shape, use np.ones_like, np.zeros_like, or np.full_like.

python
1import numpy as np
2
3data = np.arange(12).reshape(3, 4)
4
5mask_true = np.ones_like(data, dtype=bool)
6mask_false = np.zeros_like(data, dtype=bool)
7
8print(mask_true.shape)
9print(mask_false.shape)

This is common in masking code, where the boolean array should track the shape of an existing dataset exactly.

You can also use np.full_like(data, True, dtype=bool) if that reads more clearly in your project.

Use boolean arrays for masks and initialization

These arrays are rarely the final result. They are usually starting points for mask construction.

For example, begin with all True and then turn off positions that fail some condition:

python
1import numpy as np
2
3values = np.array([5, 12, 3, 20, 8])
4mask = np.ones(values.shape, dtype=bool)
5mask[values < 10] = False
6
7print(mask)
8print(values[mask])

This prints the elements greater than or equal to 10.

You can also start with all False and mark positions as they become eligible. That pattern is common in graph algorithms, image masks, and iterative filtering pipelines.

Avoid np.empty for initialized boolean arrays

np.empty creates an array without initializing its contents. That is useful when you plan to assign every element immediately, but it is the wrong tool when you want "all true" or "all false."

python
1import numpy as np
2
3dangerous = np.empty((2, 2), dtype=bool)
4print(dangerous)

The output is unpredictable because the memory is not initialized. If you need consistent boolean values, use np.full, np.ones, or np.zeros.

Common Pitfalls

  • Forgetting dtype=bool and creating numeric arrays of 1 or 0 instead of real boolean arrays.
  • Using np.empty when you actually need every element initialized to a known boolean value.
  • Recreating an array shape manually instead of using _like helpers that match an existing array.
  • Assuming np.ones and np.zeros are semantically clearer than np.full in every codebase.
  • Building masks with Python lists first when a direct NumPy boolean array is simpler and faster.

Summary

  • Use np.ones(shape, dtype=bool) for all True and np.zeros(shape, dtype=bool) for all False.
  • Use np.full(shape, True or False, dtype=bool) when you want the initializer value to be explicit.
  • Use _like variants when the new boolean array should match another array's shape.
  • Boolean arrays are most useful as masks and selection helpers.
  • Avoid np.empty unless you are about to assign every value yourself.

Course illustration
Course illustration

All Rights Reserved.