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

How to convert List of Lists to NumPy Array in Python

In this article, you will learn how to convert a list of lists into a NumPy array using numpy.array().

In this article, you will learn how to convert a list of lists into a NumPy array using numpy.array().

Step 1: Create a new list of lists or nested lists

ListofLists = [[1, 2, 3, 4, 5], [6, 7, 8, 9 ,10]]

# Display lists
print(ListofLists)

# Display type
print(type(ListofLists))

Output:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
<class 'list'>

Step 2: Convert a list of lists into a numpy array

# import required python library
import numpy

# You can convert it using numpy.array
my_array = numpy.array(ListofLists)

# Display converted nested arrays
print(my_array)

# Display type
print(type(my_array))

Output:

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
<class 'numpy.ndarray'>

Related Article:

You may read the following article for the conversion of the simple list into a NumPy array.

References:

Check out the NumPy original documentation for a brief description.

This Post Has One Comment

Leave a Reply