NumPy Tutorial NumPy Statistics NumPy References

NumPy - asarray() function



The NumPy asarray() function is used to convert the input to an array. The syntax for using this function is given below:

Syntax

numpy.asarray(a, dtype=None, order=None)

Parameters

a Required. Specify the input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
dtype Optional. Specify the desired data type. By default, the data-type is inferred from the input data.
order Optional. Specify whether to store the result. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Return Value

Returns an array interpretation of a.

Example: Create numpy array

In the example below, the asarray() function is used to create a numpy array from an existing data.

import numpy as np

x1 = [10, 20, 30, 40, 50, 60]
x2 = (100, 200, 300)
x3 = [[10, 20, 30], [40, 50, 60]]

#creating numpy array from a list
Arr1 = np.asarray(x1)
print("Arr1 is:", Arr1)

#creating numpy array from a tuple
Arr2 = np.asarray(x2, dtype=float)
print("\nArr2 is:", Arr2)

#creating numpy array from a list of list
Arr3 = np.asarray(x3)
print("\nArr3 is:\n", Arr3)

The output of the above code will be:

Arr1 is: [10 20 30 40 50 60]

Arr2 is: [100. 200. 300.]

Arr3 is:
[[10 20 30]
 [40 50 60]]

❮ NumPy - Functions