Celebrate Our 22nd Anniversary with Huge Savings! Up to 70% Off

How to Convert NumPy Arrays to Lists in Python?

Numpy arrays are a part of the Python numpy library and are a little different as they are specially optimized for numerical computations. They are faster than standard Python arrays, providing memory efficiency. 

Yet most of the time, you'll have to convert NumPy arrays to Python lists to work with other libraries, serialize data, and easily handle them.

Regardless of whether you are scripting on your local machine or have a Python VPS setup, the methods included will help you to handle NumPy data easily.

 

Prerequisites

  • Python 3.x is installed on your system or VPS
  • NumPy package installed
  • Basic understanding of Python and NumPy arrays

If you're running an Ubuntu VPS for your Python applications, you can set up your environment with:

 
# Update your Ubuntu VPS packages

sudo apt update

sudo apt upgrade -y

 
# Install Python and pip

sudo apt install python3 python3-pip -y

 

# Install NumPy

 
apt install python3-numpy

 

Basic NumPy Array to List Conversion

 

Method 1: Using tolist() Method (Recommended)

The most efficient and recommended way to convert a NumPy array to a Python list is using the tolist() method:

 
 

import numpy as np


# Create a simple 1D NumPy array

array_1d = np.array([1, 2, 3, 4, 5])

print(f"Original NumPy array:\n{array_1d}")

 
Output:

 
 

# Convert to Python list

list_1d = array_1d.tolist()

print(f"Converted list: {list_1d}")

 
Output:

 

The tolist() method is specially optimized for NumPy arrays and preserves the array structure while converting NumPy scalar types to Python scalar types.

 

Method 2: Using list() Function (Less Efficient)

You can also use the built-in list() function:

 
 

import numpy as np


# Create a NumPy array

array_1d = np.array([1, 2, 3, 4, 5])


# Convert using list() function

list_1d_alt = list(array_1d)

print(f"Converted using list(): {list_1d_alt}")

 
Output:

 

While list() works for simple arrays, it's significantly slower than tolist() for large arrays and doesn't handle complex nested arrays as efficiently.

 

Converting Multi-dimensional Arrays

Converting 2D NumPy Arrays to Nested Lists

Working with 2D arrays is common in data science applications:

 
 

import numpy as np


# Create a 2D NumPy array

array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(f"Original 2D NumPy array:\n{array_2d}")

 
Output:

 
 

# Convert to nested Python list

list_2d = array_2d.tolist()

print(f"Converted nested list: {list_2d}")

 
Output:

 

Multi-dimensional arrays are common in data processing pipelines(automated steps), and preserving their structure is more important for downstream applications(processing data from other applications).

 

Converting 3D and Higher-Dimensional Arrays

The same method works for arrays of any dimension:

 
 

import numpy as np


# Create a 3D NumPy array

array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print(f"Original 3D NumPy array:\n{array_3d}")

 
Output:

 
 

# Convert to a deeply nested Python list

list_3d = array_3d.tolist()

print(f"Converted deeply nested list: {list_3d}")

 
Output:

 

Higher-dimensional arrays are common in machine learning applications, and proper conversion keeps all the structural relationships as it is in your data.

 

Advanced Operations

 

Flattening Arrays Before Conversion

Sometimes you need a flat list instead of a nested structure:

 
 

import numpy as np


# Create a 2D NumPy array

array_2d = np.array([[1, 2, 3], [4, 5, 6]])


# Flatten array first, then convert to list

flat_list = array_2d.flatten().tolist()

print(f"Flattened list: {flat_list}")

 
Output:

 

Flattening (multi to single dimensional) is essential when you need to process all elements sequentially without regard to their original structure.

 

Converting Arrays with Mixed Data Types

Real-world data often contains mixed types, and proper conversion preserves these variations.

NumPy can handle arrays with mixed data types:

 
 

import numpy as np


# Create a NumPy array with mixed types

mixed_array = np.array([1, 'hello', 3.14, True])

print(f"Mixed type NumPy array:\n{mixed_array}")


# Convert to Python list

mixed_list = mixed_array.tolist()

print(f"Converted mixed list: {mixed_list}")

 
Output:

 

Further, you can separate each element from the list as per its data type:

 
 

strings = []

integers = []

floats = []

booleans = []


for item in mixed_list:

    try:

        # Try boolean first, but skip if it's a number string like '1'

        if item in ['True', 'False']:

            booleans.append(eval(item))

        # Try integer

        elif item.isdigit():

            integers.append(int(item))

        # Try float

        elif '.' in item:

            floats.append(float(item))

        # Otherwise, it's a string

        else:

            strings.append(item)

    except:

        strings.append(item)  # Fallback in case something can't be parsed


print("Strings:", strings)

print("Integers:", integers)

print("Floats:", floats)

print("Booleans:", booleans)

 
Output:

 

Performance Considerations

For Ubuntu VPS users:

Faster conversions = better server performance!

 
 

Comparing tolist() vs list()

When working on a Python VPS hosting environment, even with a good amount of resources, performance matters:

 
 

import numpy as np

import time


# Create a large NumPy array

large_array = np.random.rand(100000)


# Compare execution time: tolist()

start_time = time.time()

list_via_tolist = large_array.tolist()

tolist_time = time.time() - start_time


# Compare execution time: list()

start_time = time.time()

list_via_list = list(large_array)

list_time = time.time() - start_time


print(f"tolist() execution time: {tolist_time:.6f} seconds")

print(f"list() execution time: {list_time:.6f} seconds")

print(f"tolist() is {list_time/tolist_time:.2f}x faster")

 
Output:

 

In resource-constrained VPS environments, choosing the most efficient method can impact your application's performance, at the time when processing large datasets.

 

Troubleshooting Common Issues

 

Issue 1: Memory Errors with Large Arrays

When converting very large arrays on a VPS with limited memory:

 
# Problem: Memory error with huge arrays

try:

    huge_array = np.ones((10000, 10000))

    huge_list = huge_array.tolist()  # May cause memory issues

except MemoryError:

    print("Memory error encountered!")

 
# Solution: Process in chunks

def convert_in_chunks(array, chunk_size=1000):

    result = []

    for i in range(0, len(array), chunk_size):

        chunk = array[i:i+chunk_size].tolist()

        result.extend(chunk)

    return result

 
# Example usage

medium_array = np.ones(1000000)

chunked_list = convert_in_chunks(medium_array, 100000)

print(f"Successfully converted {len(chunked_list)} elements")

 

On VPS environments with memory constraints, processing large arrays in chunks prevents out-of-memory errors.

 

Issue 2: Precision Loss with Floating-Point Numbers

Floating-point conversion may lead to precision issues:

 
 

import numpy as np


# Create a NumPy array with floating-point values

float_array = np.array([1.1, 2.2, 3.3, 4.4, 5.5])


# Convert to list

float_list = float_array.tolist()


# Check for precision differences

for np_val, list_val in zip(float_array, float_list):

    if np_val != list_val:

        print(f"NumPy value: {np_val}, List value: {list_val}")

    else:

        print(f"Values match: {np_val}")

 

Being aware of potential precision differences is crucial for scientific computing and financial applications.

 

Issue 3: Structure Loss with Special NumPy Arrays

Some special NumPy arrays might lose their special properties:

 
 

import numpy as np


# Create a masked array

masked_array = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 0])

print(f"Masked array: {masked_array}")


# Convert to list

masked_list = masked_array.tolist()

print(f"Converted list: {masked_list}")

print("Note: Masking information is lost!")

 
# Solution: Convert with consideration for masks

def masked_to_list(masked_array, fill_value=None):

    result = []

    for i, value in enumerate(masked_array):

        if masked_array.mask[i]:

            result.append(fill_value)

        else:

            result.append(value)

    return result

custom_list = masked_to_list(masked_array, fill_value="MASKED")

print(f"Custom conversion: {custom_list}")

 

Special NumPy array types may require custom conversion logic to preserve their unique properties.

 

Best Practices for Python VPS Environments

1. Memory Management: On Ubuntu VPS hosting with limited RAM, large arrays can be processed in chunks.

2. Performance Optimization: Always use tolist() instead of list() for large arrays.

3. Error Handling: Add proper error handling for memory-intensive operations.

4. Caching: For repeated conversions, cache results to avoid redundant processing.

The performance benefits of using optimized methods like tolist() become especially important when processing large datasets on resource-constrained systems like a budget Ubuntu VPS.


Was this answer helpful?

« Back

chat