NumPy
ndarray
array
Python
programming

What is the difference between ndarray and array in NumPy?

Master System Design with Codemia

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

Introduction

In NumPy discussions, array and ndarray are often used as if they mean the same thing. They are related, but not identical. ndarray is the actual NumPy array type, while np.array(...) is one of the factory functions that creates an ndarray from other data.

ndarray Is the Core NumPy Data Type

ndarray stands for N-dimensional array. It is the class that stores the array's shape, data type, memory layout, and values.

python
1import numpy as np
2
3values = np.array([1, 2, 3])
4print(type(values))
5print(values.ndim)
6print(values.shape)
7print(values.dtype)

When you run this code, type(values) is numpy.ndarray. That is the key point: the thing you manipulate in NumPy is usually an ndarray instance.

ndarray supports vectorized operations, broadcasting, slicing, reshaping, and many of the features that make NumPy fast.

np.array(...) Is a Constructor Function

When people say "a NumPy array," they often mean an object created by np.array(...).

python
1import numpy as np
2
3python_list = [1, 2, 3]
4arr = np.array(python_list)
5print(arr)
6print(type(arr))

Here, np.array(...) is a function that takes list-like input and returns an ndarray. It is similar to how list(...) creates a Python list or dict(...) creates a dictionary.

That means the difference is mostly one of role:

  • 'ndarray is the class'
  • 'np.array(...) is a common way to create an instance of that class'

The Name array Can Also Mean Other Things

The confusion gets worse because Python also has an array module in the standard library.

python
1from array import array
2import numpy as np
3
4py_array = array('i', [1, 2, 3])
5np_array = np.array([1, 2, 3])
6
7print(type(py_array))
8print(type(np_array))

These are different types with different goals. The standard-library array.array is a compact one-dimensional container. NumPy's ndarray supports multidimensional data and numerical operations that the standard library type does not.

So when someone writes "array," context matters. They might mean the generic idea of an array, the np.array(...) constructor, the resulting ndarray, or even array.array from the standard library.

np.array(...) Versus np.asarray(...)

A practical distinction that matters more in real code is between np.array(...) and np.asarray(...).

python
1import numpy as np
2
3original = np.array([1, 2, 3])
4a = np.array(original)
5b = np.asarray(original)
6
7print(a is original)
8print(b is original)

np.array(...) may copy data unless you tell it not to. np.asarray(...) tries to avoid copying when the input is already an ndarray with a suitable dtype.

This matters for performance, memory use, and whether modifications affect the original object.

Why ndarray Matters More Than the Constructor

Most NumPy behavior lives on the ndarray object itself. Attributes such as shape, strides, dtype, and methods such as reshape, sum, and astype belong to the array instance.

python
1import numpy as np
2
3matrix = np.array([[1, 2], [3, 4]])
4print(matrix.reshape(4))
5print(matrix.sum(axis=0))

Once the object exists, the fact that it was originally created by np.array(...) is usually no longer important. What matters is that it is an ndarray.

Use the Right Mental Model

A clean mental model is:

  • NumPy's main array object is numpy.ndarray
  • 'np.array(...) is a convenience function that usually creates one'
  • the word "array" in conversation is often informal shorthand

This model helps when reading documentation, debugging types, and understanding performance behavior. For example, a function that expects an ndarray may still accept any array-like input because NumPy can convert it internally.

Common Pitfalls

  • Thinking array and ndarray are two competing NumPy container types.
  • Forgetting that np.array(...) is a constructor function, not the underlying type name.
  • Mixing up NumPy arrays with array.array from Python's standard library.
  • Assuming np.array(...) never copies data.
  • Using the word "array" too loosely when type details matter.

Summary

  • 'ndarray is NumPy's actual N-dimensional array class.'
  • 'np.array(...) is a function that commonly creates an ndarray.'
  • In casual discussion, people often use "array" as shorthand for a NumPy ndarray.
  • Python's standard-library array.array is a different type.
  • For performance-sensitive code, it is useful to understand how np.array(...) differs from np.asarray(...).

Course illustration
Course illustration

All Rights Reserved.