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.
Output:
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.
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.
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:
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."
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=booland creating numeric arrays of1or0instead of real boolean arrays. - Using
np.emptywhen you actually need every element initialized to a known boolean value. - Recreating an array shape manually instead of using
_likehelpers that match an existing array. - Assuming
np.onesandnp.zerosare semantically clearer thannp.fullin 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 allTrueandnp.zeros(shape, dtype=bool)for allFalse. - Use
np.full(shape, True or False, dtype=bool)when you want the initializer value to be explicit. - Use
_likevariants when the new boolean array should match another array's shape. - Boolean arrays are most useful as masks and selection helpers.
- Avoid
np.emptyunless you are about to assign every value yourself.

