How to Convert Python Lists to NumPy Arrays and Vice Versa

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

Introduction

NumPy is a powerful library for numerical computing in Python. It provides support for arrays which are more efficient and offer more functionality than native Python lists when processing numerical data. This tutorial will guide you through the process of converting Python lists to NumPy arrays and converting NumPy arrays back to Python lists.

Before we dive into conversions, make sure you have NumPy installed:

pip install numpy

Converting Python Lists to NumPy Arrays

To convert a Python list to a NumPy array, you can use the numpy.array function:

import numpy as np

# Python list
def get carry out = [1.0, 2.0, 3.0]

# Convert list to NumPy array
np_array = np.array(my_list)

# Output NumPy array
print(np_array)
print(type(np_array))

Output:

[1. 2. 3.]
<class 'numpy.ndarray'>

It’s straightforward, and now you have an array that can be used for complex calculations.

Multidimensional List Conversion

Converting multidimensional lists (lists of lists) is just as simple:

# Multidimensional Python list
list_of_lists = [[1, 2], [3, 4], [5, 6]]

# Convert to a NumPy matrix (2D array)
np_2d_array = np.array(list_of_lists)

# Output 2D array
print(np_2d_array)

Output:

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

This gives you a NumPy 2D array, which is useful for matrix operations and higher-dimensional data processing.

Array Attributes

Once you have an array, several attributes will help you understand its structure:

# Use the shape, ndim, and dtype attributes
print('Array Shape:', np_2d_array.shape)
print('Array Dimensions:', np_2d_array.ndim)
print('Array Data Type:', np_2d_array.dtype)

Understanding these attributes is key to working effectively with NumPy arrays.

Converting NumPy Arrays to Python Lists

Converting a NumPy array back to a Python list is also straightforward using the tolist() method:

# Convert NumPy array to Python list
python_list = np_array.tolist()

# Output Python list
print(python_list)
print(type(python_list))

Output:

[1.0, 2.0, 3.0]
<class 'list'>

This method works for higher-dimensional arrays as well.

Data Type Considerations

NumPy automatically chooses the most suitable data type when converting lists. However, you can specify the data type:

# Create a NumPy array of integers
np_int_array = np.array(my_list, dtype='int32')

# Output the new array and its data type
print(np_int_array)
print(np_int_array.dtype)

Output:

[1 2 3]
int32

Specifying dtype ensures the NumPy array has the data type you need for your calculations.

Advanced: Nested Structures and Arrays

In practice, you may need to convert more complex nested structures:

# Creating a nested list with sublists of different lengths
jagged_list = [[1, 2, 3], [4, 5], [6]]

# Use list comprehension to convert to an array of arrays
np_objects_array = np.array([np.array(x) for x in jagged_list])

# Output the object array of NumPy arrays
print(np_objects_array)
print(np_objects_array.dtype)

Output:

[array([1, 2, 3]) array([4, 5]) array([6])]
object

Each sublist becomes its separate NumPy array within a higher-level array of objects, which can be useful for complex data handling scenarios.

Conclusion

Learning to convert between Python lists and NumPy arrays is fundamental for anyone working with numerical data in Python. While Python lists offer more general-purpose functionality, NumPy arrays provide optimized performance and additional capabilities for scientific computing. With the techniques demonstrated in this tutorial, you can efficiently transition between Python lists and NumPy arrays according to the requirements of your computational task.