NumPy array() function

In this tutorial, we will cover the concept of array() function in the NumPy library.

The array() function in the NumPy library is mainly used to create an array. Just like the Numpy arange() function.

  • In the NumPy library the homogeneous multidimensional array is the main object. Homogeneous multidimensional array is basically a table of elements and these elements are all of the same type and indexed by a tuple of positive integers. The dimensions of an array are called as axis in NumPy.
  • The array class in the Numpy library is mainly known as ndarray or an alias array.
  • The numpy.array() is not same as the standard Python library class array.array because in python array.array is used to handle only one-dimensional arrays and thus it provides less functionality.

Syntax of numpy.array():

The syntax required to use this method is as follows:

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


Let us now take a look at the parameters of this function:

  • object This parameter indicates an array or it can be any object exposing the array interface mainly it can be an object whose __array__ method returns an array, or any nested sequence.
  • dtype This parameter defines the desired data-type for the array. In case if this parameter is not given, then the type will be determined as the minimum type required to hold the objects in the sequence.
  • copy If value of this parameter is set to True, the object will be copied else the copy will be made only when an object is a nested sequence, or if a copy is just needed to satisfy any of the other requirements such as dtype, order, etc.
  • order This prameter is used to specify the memory layout of the array. In case if the object is not an array then the newly created array will be in C order that is row major unless ‘F’ is specified, in that case it will be in Fortran order that is column order. If the object is mainly an array then the following holds:
OrderNo Copycopy=true
‘K’unchangedIf copy is true in this case then F & C order preserved otherwise most similar order
‘A’unchangedIn this case if copy is true and input is F not C then the order is F otherwise the order is C
‘C’C orderIn this case the order is C
‘F’F orderIn this case the order is F.
  • subok This is an optional parameter, when the value of this parameter is True, then sub-classes will pass-through otherwise by default the returned array will force to be a base-class array.
  • ndmin It is an optional parameter that is used to specify the minimum number of dimensions that the resulting array should have.

Returned Values:

The array() function in the numpy library is used to return an array object that satisfies the specified requirements.

Example 1: Basic example of array() function

Below we have a basic example where we create an array having only one dimension with the help of array() function:

import numpy as np  

a = np.array([1,4,9])  
print("The Array is:")


The Array is: 
[1 4 9]

Example 2: Multi-dimension array

Now we will create an array having more than one dimension and the code for the same is as follows:

import numpy as np  

a = np.array([[1, 7], [6, 4]])  
print("The Array with more than one dimension:")


The Array with more than one dimension:
 [[1 7] [6 4]]

Example 3: Using the dtype Parameter

Below we have an example where we will use the dtype parameter:

import numpy as np  

a = np.array([1, 7,9], dtype='complex')  
print("The Array with more than one dimension:")


The Array with more than one dimension: 
[1.+0.j 7.+0.j 9.+0.j]

Note: The output of the above code snippet indicates the values of the array elements in the form of complex numbers.

Follow Us On