# In numpy, what is the difference between ndarray and array?

## Problem

In Numpy, what is the difference between ndarray and array? And where in the numpy source code can I locate the implementations?

## Solution #1

Numpy.array is only a helper function for making an ndarray; it isn’t a class in and of itself.

You can alternatively use numpy.ndarray to make an array, however this is not advised. Numpy.ndarray’s docstring says:

The guts of the implementation is in C code, which you can see here in multiarray, but you may start with the ndarray interfaces:

## Solution #2

The function numpy.array returns a numpy.ndarray. Numpy.array is not an object type.

Answered by Ramón J Romero y Vigil

## Solution #3

A few lines of code to illustrate the differences between numpy.array and numpy.ndarray.

Create a list as a warm-up step.

``````a = [1,2,3]
``````

Check the type

``````print(type(a))
``````

You will get

``````<class 'list'>
``````

Using np.array, create an array (from a list).

``````a = np.array(a)
``````

You can also skip the warm-up and go straight to the workout.

``````a = np.array([1,2,3])
``````

Check the type

``````print(type(a))
``````

You will get

``````<class 'numpy.ndarray'>
``````

This indicates that the numpy array’s type is numpy.ndarray.

You can also determine the type by using the following formula:

``````isinstance(a, (np.ndarray))
``````

``````True
``````

An error warning will appear if you type either of the following two lines.

``````np.ndarray(a)                # should be np.array(a)
isinstance(a, (np.array))    # should be isinstance(a, (np.ndarray))
``````

## Solution #4

Numpy.ndarray() is a class, whereas numpy.array() is a method for creating ndarray.

According to the numpy documentation, there are two ways to generate an array from the ndarray class:

1- build arrays with array(), zeros(), or empty() methods: Arrays should be built with array(), zeros(), or empty() methods (refer to the See Also section below). The parameters here correspond to a low-level array instantiation mechanism (ndarray(…)).

2- straight from the ndarray class: When using __new__ to create an array, there are two options: Only shape, dtype, and order are used if buffer is None. All keywords are processed if buffer is an object that exposes the buffer interface.

Because we didn’t assign a buffer value, the next example produces a random array:

Assigning an array object to the buffer is another example:

We can’t assign a list to “buffer” in the example above, therefore we had to use numpy.array() to return an ndarray object for the buffer.

If you wish to build a numpy.ndarray() object, use numpy.array().”