How to Convert a List to a NumPy Array?

Python list (dynamic array) is used to store the collection of various types of data, unlike arrays that are used to store the similar type of data. There are a number of ways to initialize arrays in Python programs such as using the numpy module, array module, etc. But sometimes we need to convert a Python list into an array data structure. For this purpose, various functions are used.

In this tutorial different methods are used to convert a list to a numpy array. The given below contents will be elaborated with numerous examples in this Python tutorial:

Method 1: Using numpy.array() 

The “numpy.array()” function is used to represent the collection of data having similar data types. The following examples are used to convert the list or list of the list to a numpy array:

Example 1: List to Numpy Array

The given below code is used to convert the list to a numpy array:

Code:

import numpy
list_value = [55, 45, 65, 85, 98]
print(list_value)
print(type(list_value))

array_value = numpy.array(list_value)
print ("\nNumpy-Array : ", array_value)
print(type(array_value))
  • The “numpy” module is imported into the program.
  • The list named “list_value” is initialized.
  • The “numpy.array()” function takes the given list value as a parameter and retrieves the array.

Output:

The given list has been converted into a numpy array.

Example 2: List of List to Numpy Array

The below code is utilized to convert the Python list of lists to a numpy array:

Code:

import numpy
list_value = [[55, 45, 65],[88, 98, 78]]
print(list_value)
print(type(list_value))

array_value = numpy.array(list_value)
print ("\nNumpy-Array : ", array_value)
print(type(array_value))
  • The “list of list” named “list_value” is initialized in the program.
  • The “numpy.array()” accepts the list of lists as an argument and returns the numpy array.

Output:

The given list has been converted into a numpy array.

Method 2: Using numpy.asarray() 

The “numpy.asarray()” function is used to convert iterable like lists, tuples, lists of lists, etc. to a numpy array in Python. 

We use this function in the following examples to convert the given list into a numpy array:

Example 1: List to Numpy Array

The following code is used to convert the given Python list to a numpy array:

Code:

import numpy
list_value = [55, 45, 65, 88, 98, 78]
print(list_value)
print(type(list_value))

array_value = numpy.asarray(list_value)
print ("\nNumpy-Array : ", array_value)
print(type(array_value))
  • The list named “list_value” is initialized.
  • The “numpy.asarray()” function accepts the “list_value” as a parameter and converts it into a numpy array.

Output:

The list has been successfully converted into a numpy array.

Example 2: List of List to Numpy Array

The following code converts the “list of lists” to a numpy array utilizing the “numpy.asarray()” function:

Code:

import numpy
list_value = [[55, 45],[65, 88], [98, 78]]
print(list_value)
print(type(list_value))

array_value = numpy.asarray(list_value)
print ("\nNumpy-Array : ", array_value)
print(type(array_value))
  • The list of lists named “list_value” is initialized in the program.
  • The “numpy.asarray()” function accepts the list of list variables and returns the numpy array.

Output:

The input list of the list has been converted into a numpy array.

Conclusion

To convert a given list or list of lists to a Numpy array, the “numpy.array()” and the “numpy.asarray()” functions are used in Python. The “numpy.array()” and  “numpy.asarray()” functions accept the list as an argument and return the numpy array. This guide delivered various ways to convert a Python list to a numpy array using appropriate examples.