×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

How to Convert List of Lists to NumPy Array?

In this tutorial, we will learn how to convert list of lists to NumPy array? By Pranit Sharma Last updated : May 26, 2023

Problem Statement

Given a Python list of lists (i.e., nested list), we have to convert it to NumPy array.

Converting List of Lists to NumPy Array

To convert list of lists to NumPy array, just use numpy.array() method and pass the object of the list of lists. This method will accept the list of lists and create a NumPy array. Consider the following code snippets for converting list of lists to NumPy array:

arr = np.array(l)

Where, l is the list of lists.

Let us understand with the help of an example,

Python Program to Convert List of Lists to NumPy Array

# Import NumPy
import numpy as np

# Creating a nested list (list of lists)
l = [[1, 2], [3, 4], [5, 6]]

# Printing list of lists and its type
print("List of lists (l):\n", l, "\n")
print("Type of l:", type(l), "\n")

# Converting list of lists to NumPy array
arr = np.array(l)

# Printing NumPy array
print("Result: NumPy Array (arr):\n", arr, "\n")
print("Type of arr:", type(arr), "\n")

Output

List of lists (l):
 [[1, 2], [3, 4], [5, 6]] 

Type of l: <class 'list'> 

Result: NumPy Array (arr):
 [[1 2]
 [3 4]
 [5 6]] 

Type of arr: <class 'numpy.ndarray'>

Explanation

See the program and output – l is the list of lists containing some values and we have converted it into NumPy array (arr) by using this numpy.array(), we also printed the type of both of the objects l and arr for better understanding.

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.