You are currently viewing How to convert List into NumPy Array in Python

How to convert List into NumPy Array in Python

In this article, you will learn how to convert a python list into a NumPy array using numpy.array.

In this article, you will learn how to convert a python list into a NumPy array using numpy.array.

Step 1: Create a new list that has 6 elements

my_list = [3, 2, 4, 5, 5, 10]

# Display list
print(my_list)

# Display type
print(type(my_list))

Output:

[3, 2, 4, 5, 5, 10]
<class 'list'>

Step 2: Convert the List into a NumPy array using numpy.array()

# import required python library
import numpy

# numpy.array can convert a List into NumPy array
my_array = numpy.array(my_list)

# Display numpy array
print(my_array)

# Display type
print(type(my_array))

Output:

[ 3  2  4  5  5 10]
<class 'numpy.ndarray'>

Related Article:

You may read the following article for the conversion of the list of lists or nested lists into a NumPy array.

References:

Check out the NumPy original documentation for a brief description.

This Post Has One Comment

Leave a Reply