How to convert a list in NumPy array

You can convert a list in the NumPy array with the following code. If you want to learn Python, I highly recommend reading This Book.

How to convert a list in NumPy array
How to convert a list in NumPy array

Method 1: Convert a list in NumPy array using np.array()

import numpy as np
a = [1, 2, 3, 4]
b = np.array(a)

# display type of a
print(type(a))

# display type of b
print(type(b))
<class 'list'>
<class 'numpy.ndarray'>

Method 2: Convert a list in NumPy array using np.asarray()

import numpy as np
a = [1, 2, 3, 4]
b = np.asarray(a)

# display type of a
print(type(a))

# display type of b
print(type(b))
<class 'list'>
<class 'numpy.ndarray'>

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.