How to turn a nested Python list of lists into a NumPy 2D array

Updated: January 23, 2024 By: Guest Contributor Post a comment

Overview

NumPy is a fundamental package for scientific computing in Python. It provides support for arrays that are more efficient and therefore more suitable for scientific computation than Python’s native list structures. In this tutorial, we’ll walk through how to convert a nested Python list of lists into a NumPy 2D array step by step, including several code examples that increase in complexity.

Before we dive into the conversion, make sure that NumPy is installed in your working environment. If it is not installed yet, you can install it using pip:

pip install numpy

Basic Conversion

The simplest form of conversion is to use the numpy.array function on a list of lists:

import numpy as np

# Define a list of lists
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Convert to a NumPy 2D array
numpy_array = np.array(nested_list)

# Print the result
print(numpy_array)

This code will output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Ensuring Uniformity of Data Types

One key behavior of NumPy arrays is that they would contain items of the same data type. This contrasts with Python lists which can hold mixed types. When converting, NumPy will try to find a type that all elements can be safely cast to:

import numpy as np

# Define a list of lists with mixed types
mixed_list = [[1, '2', 3], [4.0, '5', 6], [7, 8.0, '9']]

# Convert to a NumPy 2D array
numpy_array = np.array(mixed_list)

# Print the result
print(numpy_array)

NumPy will upcast integers to floats, or numbers to strings, to ensure all elements have the same type. The output here would be a 2D NumPy array with all elements cast to strings:

[['1' '2' '3']
 ['4.0' '5' '6']
 ['7' '8.0' '9']]

Handling Lists of Unequal Length

NumPy arrays, unlike Python lists, require that all subarrays (or rows in the 2D case) have the same length. If not, NumPy will still create an array, but it will be an array of objects:

import numpy as np

# Define a list of lists with different lengths
unequal_list = [[1], [2, 3], [4, 5, 6]]

# Convert to a NumPy 2D array
numpy_array = np.array(unequal_list)

# Print the result
print(numpy_array)

The resulting NumPy object may look like an array, but it will essentially be an array of lists, not a 2D array.

Converting with the ‘dtype’ Parameter

When calling np.array, you can also specify the desired data type with the dtype parameter. This can be helpful if you want to ensure that integers remain integers or if you need to specify complex numbers:

import numpy as np

# Define a list of lists
nested_list = [[1, 2, 3], [4, 5, 6]]

# Convert to an integer NumPy 2D array
int_array = np.array(nested_list, dtype=int)

# Print the result
print(int_array)

This will output:

[[1 2 3]
 [4 5 6]]

Advanced Operations

Let’s look at how to perform more sophisticated transformations when converting lists of lists into NumPy 2D arrays. For example, what if you need to transpose your matrix, or you need to perform some operations on it during conversion?

Transposing Your Matrix

If you wish to transpose the resulting 2D array (swap rows with columns), you can make use of the .T attribute:

import numpy as np

# Define a list of lists
nested_list = [[1, 2, 3], [4, '5.5', 6], [7, 8, '9']]

# Convert to a NumPy 2D array
numpy_array = np.array(nested_list)

# Transpose the matrix
tranposed_array = numpy_array.T

# Print the result
print(tranposed_array)

The output of this would be:

[['1' '4' '7']
 ['2' '5.5' '8']
 ['3' '6' '9']]

Handling Complex Numbers

When you have a nested list that contains complex numbers, you can specify this with the dtype parameter as ‘complex’:

import numpy as np

# Define a list of lists with complex numbers
complex_list = [[1+2j, 2], [3, 4-1j]]

# Convert to a complex NumPy 2D array
complex_array = np.array(complex_list, dtype=complex)

# Print the result
print(complex_array)

This code snippet outputs:

[[1.+2.j 2.+0.j]
 [3.+0.j 4.-1.j]]

The above-mentioned steps provide a solid foundation for converting nested Python lists into NumPy 2D arrays with a deep understanding of the mechanisms involved. Conversions discussed include basic transformations, uniformity considerations, handling uneven sub-lists, specifying data types, transposing matrices, and dealing with complex numbers.

Conclusion

In this tutorial, we covered multiple aspects of converting a nested Python list of lists to a NumPy 2D array. By understanding how NumPy handles different scenarios and using the appropriate parameters, one can efficiently manipulate, transform, and apply computation on large data sets within the NumPy framework.