Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.array

Creates an ndarray from an array-like object, such as a nested sequence or compatible array object. Optional arguments control dtype, copying, memory order, subclass handling, and dimensions.

How it works

numpy.array converts an array-like input into an ndarray, optionally controlling its data type, memory layout, subclass behavior, and dimensionality. It can accept nested sequences, existing arrays, array-interface objects, or objects implementing __array__.

Syntax

numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, ndmax=0, like=None)

Parameters

NameTypeDescription
objectarray_likeInput array, array-interface object, object with __array__, or nested sequence.
dtypedata-type, optionalDesired data type; if omitted, NumPy infers one that represents the values.
copybool, optionalWhether to copy the input; defaults to True.
order{'K', 'A', 'C', 'F'}, optionalMemory layout choice: K, A, C, or F; defaults to K.
subokbool, optionalWhether to pass through ndarray subclasses; defaults to False.
ndminint, optionalMinimum number of dimensions in the result; defaults to 0.
ndmaxint, optionalMaximum number of dimensions in the result; defaults to 0.
likearray_like, optionalReference object enabling array creation through a compatible array-like implementation; defaults to None.

Returns

An ndarray containing the converted input values.

Key points

  • The function constructs an ndarray from array-like input.
  • Nested Python sequences can define multidimensional array structure.
  • Use dtype to request the element type explicitly.
  • ndmin adds dimensions when needed so the result reaches the requested minimum rank.
  • order selects the array's memory-layout preference.
  • Existing arrays and array-interface-compatible objects are accepted as inputs.

Common mistakes

  • For non-array inputs, the default layout produces C-order storage unless Fortran order is requested.
  • The default memory-order setting is K; choose another order when a specific layout is required.
  • Without dtype, NumPy infers a type capable of representing the input values, which can promote mixed numeric inputs.

Example: Create arrays from sequences

import numpy as np

values = np.array([1, 2, 3.0], dtype=np.float32)
column = np.array([1, 2, 3], ndmin=2)

print(values)
print(values.dtype)
print(column.shape)

Creates an explicitly typed one-dimensional array and a two-dimensional array with a minimum dimension.

When to use it

Build numeric arrays

Converting Python sequences for vectorized numerical operations.

Set an explicit dtype

Ensuring predictable element representation for computation or storage.

Create multidimensional arrays

Turning nested sequences into matrices or higher-dimensional data.

Control layout and dimensions

Meeting downstream layout or shape requirements.

Interview questions

QWhat kinds of input can numpy.array convert?

It accepts an array-like object and returns an ndarray. The input may be an array, an object exposing the array interface, an object whose __array__ method returns an array, or a nested sequence.

Follow-up: How would you ensure at least two dimensions? Use ndmin=2, which specifies the minimum number of dimensions for the result.

QHow is the result dtype determined?

dtype requests the desired data type. If omitted, NumPy chooses a type that can represent the values, applying promotion rules when needed.

Follow-up: What happens with [1, 2, 3.0]? The result is promoted to a floating-point type unless an explicit dtype overrides that choice.

QWhat does the order argument control?

order controls memory layout and accepts K, A, C, or F. For a non-array input, the result is C order unless F is specified.

Follow-up: When might layout matter? Algorithms or interoperability requirements may benefit from explicitly selecting C- or Fortran-order storage.

  • numpy.asarray
  • numpy.asanyarray
  • numpy.fromiter
  • numpy.zeros
  • numpy.empty
  • numpy.ndarray

Source: numpy.array — NumPy v2.5 Manual. Checked 2026-10-15.