VOOZH about

URL: https://www.geeksforgeeks.org/numpy/difference-between-np-asarray-and-np-array/

⇱ Difference between np.asarray() and np.array()? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between np.asarray() and np.array()?

Last Updated : 23 Jul, 2025

is a Python library used for dealing with arrays. In Python, we use the list inplace of the array but it’s slow to process. NumPy array is a powerful N-dimensional array object and is used in linear algebra, Fourier transform, and random number capabilities. It provides an array object much faster than traditional Python lists.

np.array in Python

The np.array() in Python is used to convert a list, tuple, etc. into a Numpy array.

Syntax: numpy.array(object, dtype=None)

Parameters :

  • object: [array_like] Input data, in any form can be converted to an array.
  • dtype: [data-type, optional] By default, the datatype is inferred from the input data.

Return : [ndarray] Array interpretation of arr.

Creating a NumPy array using the .array() function

Output:

List in python : [5, 6, 7, 9]
Numpy Array in python : [5 6 7 9]
<class 'numpy.ndarray'>

np.asarray in Python

The numpy.asarray()function is used when we want to convert the input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays.

Syntax: numpy.asarray(arr, dtype=None, order=None)

Parameters :

  • arr: [array_like] Input data, in any form can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
  • dtype: [data-type, optional] By default, the data-type is inferred from the input data.
  • order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to β€˜C’.

Return : [ndarray] Array interpretation of arr. No copy is performed if the input is already ndarray with matching dtype and order. If arr is a subclass of ndarray, a base class array is returned.

Convert a list into an array using asarray()

Output:

Input list : [1, 2, 5, 4, 6]
output array from input list : [1 2 5 4 6]
<class 'numpy.ndarray'>

Numpy array VS Numpy asarray

In Python, NumPy array and NumPy asarray are used to convert the data into ndarray. If we talk about the major difference that is when we make a NumPy array using np.array, it creates a copy of the object array or the original array and doesnot reflect any changes made to the original array. Whereas on the other hand, when we try to use NumPy asarray, it would reflect all the changes made to the original array.

Example 

Here we can see that copy of the original object is created which is actually changed and hence it's not reflected outside. Whereas, in the next part, we can see that no copy of the original object is created and hence it's reflected outside.

Output:

Original array : [2 3 4 5 6]np.array Array : [2 3 4 5 6]
np.asarray Array : [2 3 4 0 6]
Comment
Article Tags:
Article Tags:

Explore