Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

NumPy full: Create an Array Filled With a Value

How np.full builds an array of one value: the signature, dtype inference, NaN preallocation, list fill values and the full_like truncation trap.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.full returns a new array of a given shape and type, filled with a value you choose. Its signature in the current NumPy documentation is numpy.full(shape, fill_value, dtype=None, order='C', *, device=None, like=None). This guide covers the call (also searched as np.full or numpy.full), the rule that decides the output dtype, how a list passed as fill_value is repeated across rows, and how np.full differs from np.zeros, np.ones, np.empty and np.full_like.

What np.full is used for in research

The main research use is preallocation with a value that is not zero. You create the result array at its final size, then write each result into it. When the fill value is np.nan, any cell your code never reached stays visibly missing instead of passing for a real measurement of zero. That makes a half-finished loop easier to catch before it reaches a table or figure, which supports computational reproducibility. A constant array is also the usual way to build a baseline, a mask of one repeated label, or an initial value for an optimiser.

If zero is the right starting value, np.zeros is the shorter call; see the np.zeros guide. For missing-value handling after the fill, see working with NaN in NumPy.

Install or access

NumPy is installed from the official installation page at https://numpy.org/install/. The commands it gives are:

  • pip install numpy
  • conda install numpy, inside a conda environment created with conda create -n my-env and activated with conda activate my-env

The same page recommends a virtual environment: python -m venv my-env, then source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows, then pip install numpy. To confirm the install, run import numpy as np and then print(np.__version__). For the import convention, see import numpy as np.

Step-by-step: create a filled array

  1. Import the package: import numpy as np.
  2. Choose the shape. An integer gives a one-dimensional array: np.full(3, 7). A tuple gives more dimensions: np.full((2, 3), 7) is 2 rows by 3 columns.
  3. Choose the fill value. It can be a scalar, such as 10 or np.inf, or an array_like, such as [1, 2].
  4. Decide the dtype. If you leave dtype unset, NumPy infers it from the fill value (see the dtype section below). Pass dtype when you need a specific type: np.full(3, 7, dtype=np.float32).
  5. Optionally choose the memory order. order='C' (the default) is C-contiguous, row-wise in memory; order='F' is Fortran-contiguous, column-wise.
  6. Check the result with a.shape and a.dtype, and record both in your methods note.

Parameters in the current signature:

  • shape: an int or a sequence of ints, for example (2, 3) or 2.
  • fill_value: a scalar or array_like.
  • dtype: the data type; the default None means np.array(fill_value).dtype.
  • order: 'C' or 'F', default 'C'.
  • device: for Array API interoperability only; it must be "cpu" if passed. New in NumPy 2.0.0.
  • like: lets the call create an array that is not a NumPy array. New in NumPy 1.20.0.

The function returns an ndarray of the given shape, dtype and order, filled with fill_value.

Output and how to read it

The first, third and fourth rows below are the official NumPy documentation examples. All rows were also run locally on NumPy 2.5.1 on 2026-10-29.

Call Output What it shows
np.full((2, 2), np.inf) array([[inf, inf], [inf, inf]]) Any scalar works, including infinity
np.full(3, 7) array([7, 7, 7]) An integer fill value gives an integer array
np.full((2, 2), 10) array([[10, 10], [10, 10]]) A tuple sets the dimensions
np.full((2, 2), [1, 2]) array([[1, 2], [1, 2]]) A list fill value is repeated across each row
np.full((2, 3), np.nan) two rows of [nan, nan, nan] NaN preallocation; the array is float

NumPy prints each array on several lines with aligned columns; the table above shows each one on a single line for readability.

dtype: what np.full infers

When dtype is None, the documentation says the dtype is np.array(fill_value).dtype. The type of the number you pass is therefore the type of the array you get:

  • np.full(3, 7) is an integer array (int64 on the NumPy 2.5.1 build used here): array([7, 7, 7]).
  • np.full(3, 7.0) is a float array: array([7., 7., 7.]).
  • np.full(3, 7, dtype=np.float32) forces the type whatever the value: array([7., 7., 7.], dtype=float32).

This matters for two reasons in research code. First, an integer array cannot hold a fractional value later, so if you plan to write means or proportions into the array, start from a float fill such as 0.0 or np.nan, or pass dtype=float. Second, np.nan is a float value; ask for a float dtype when you fill with it. Asking for an integer dtype with NaN raised a RuntimeWarning: invalid value encountered in cast on NumPy 2.5.1, so do not rely on the result.

np.full versus np.full_like, zeros, ones and empty

The documentation lists full_like, empty, ones and zeros under See also. The one that causes surprises is np.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None, *, device=None), which returns “a full array with the same shape and type as a given array”. Because it keeps the type of a, a fill value that does not fit that type is converted. The official example uses x = np.arange(6, dtype=np.int_):

Call Result
np.full_like(x, 1) array([1, 1, 1, 1, 1, 1])
np.full_like(x, 0.1) array([0, 0, 0, 0, 0, 0]) (integer type kept, 0.1 truncated)
np.full_like(x, 0.1, dtype=np.float64) array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
np.full_like(x, np.nan, dtype=np.float64) six nan values

A second documented full_like example fills a 2 x 2 x 3 array of integers, y = np.zeros([2, 2, 3], dtype=np.int_), with np.full_like(y, [0, 0, 255]), which gives every row as [0, 0, 255]. This is the same list-repeated-across-rows behaviour as np.full, here along the last axis of a three-dimensional array. The mechanism is broadcasting in NumPy.

Troubleshooting

  • The array has the wrong type. The dtype follows the fill value. Use 7.0 instead of 7, or pass dtype.
  • The fill value was truncated. You are probably using np.full_like on an integer array. Pass dtype=np.float64, as in the example above.
  • A ValueError about broadcasting. A list fill value must be able to broadcast to the shape. np.full((2, 2), [1, 2, 3]) raised ValueError: could not broadcast input array from shape (3,) into shape (2, 2) on NumPy 2.5.1. Match the list length to the last dimension.
  • A shape given as separate numbers. The shape is one argument: write np.full((2, 3), 7), not np.full(2, 3, 7).
  • Unexpected NaN in later results. NaN spreads through arithmetic, so a leftover NaN from preallocation shows up in sums and means. To test or replace missing cells, see np.where and the NaN guide.

Free alternatives

NumPy is free and open source, and every alternative below is another NumPy function documented alongside np.full.

Function Use it when
np.zeros The starting value is 0. See the np.zeros guide.
np.ones The starting value is 1.
np.empty You will overwrite every cell and do not need a defined starting value.
np.full_like You want the shape and type of an existing array. Mind the type conversion shown above.

FAQ

What does np.full do?

It returns a new array of the shape you give, with every cell set to fill_value. The dtype comes from the fill value unless you pass dtype.

How do I fill an array with NaN?

Call np.full(shape, np.nan), for example np.full((2, 3), np.nan). NaN is a float, so the result is a float array.

How do I make a 2D array of one value?

Pass a two-element tuple as the shape: np.full((2, 3), 7) gives 2 rows and 3 columns of 7.

What happens if fill_value is a list?

The list is repeated across each row. np.full((2, 2), [1, 2]) gives array([[1, 2], [1, 2]]).

What is the difference between np.full and np.full_like?

np.full takes a shape and infers the dtype from the fill value. np.full_like takes an existing array and keeps its shape and type by default, which can truncate a fractional fill value in an integer array.

Sources

All links below were checked on 2026-10-29.

  • NumPy documentation, numpy.full (https://numpy.org/doc/stable/reference/generated/numpy.full.html), checked 2026-10-29.
  • NumPy documentation, numpy.full_like (https://numpy.org/doc/stable/reference/generated/numpy.full_like.html), checked 2026-10-29.
  • NumPy, Installing NumPy (https://numpy.org/install/), checked 2026-10-29.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy full: Create an Array Filled With a Value

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →