How to convert List into Numpy Array in Python?

Convert List into Numpy Array

To convert a List into Numpy Array in Python, call numpy.array() function and pass the list as argument to this function.

The syntax to call numpy.array() to convert a list into numpy array is

numpy.array(x)

where x is a list.

Return Value

numpy.array() returns a numpy array of type numpy.ndarray.

Example 1

In this example, we take a list in x, and convert this list into numpy array using numpy.array().

Python Program

import numpy

x = list([1, 2, 4, 8, 16, 32])
arr = numpy.array(x)

print("List :", x)
print("Numpy Array :", arr)
Run

Output

List : [1, 2, 4, 8, 16, 32]
Numpy Array : [ 1  2  4  8 16 32]

Summary

In this Numpy Tutorial of Python Examples, we learned how to convert a list into a numpy array, with the help of example program.