How to Fix a TypeError in Python When Apply User Define Function to NumPy Array

When I started experimenting with Python and NumPy, one of the first challenges I faced was applying custom transformations to arrays. It looked simple at first, but I quickly ran into a TypeError that stopped me in my tracks. I’ll walk you through the exact problem I had, why it happened, and how I fixed it. Along the way, I’ll also show you how I extended the idea into more useful functions.

The Problem Code

Here’s the code I first wrote:

import numpy as np

a = [[-0.17985, 0.178971], [-0.15312, 0.226988]]
(lambda x: x if x > 0 else np.exp(x) - 1)(a)

When I ran it, I got the following error:

TypeError: '>' not supported between instances of 'list' and 'int'

At first, this was confusing. Why couldn’t Python just apply the function to each element?

Why Does This Error Happen

The problem is that I tried to apply a lambda function directly to a, which is a list of lists.

Inside the lambda, I wrote:

x if x > 0 else np.exp(x) - 1

But x wasn’t a single number it was the entire nested list a. Python doesn’t know how to evaluate list > int, so it threw a TypeError.

I realized what I actually wanted was to apply the transformation element-wise to each number inside the array.

How I Fix It

The fix was surprisingly simple once I thought about it. I needed to:

  1. Convert a into a NumPy array.
  2. Use a NumPy function that applies conditions element-wise.

Here’s the corrected function I wrote:

import numpy as np

def f(arr):
    arr = np.array(arr)  # ensure it's a NumPy array
    return np.where(arr > 0, arr, np.exp(arr) - 1)

# Example usage
a = [[-0.17985, 0.178971], [-0.15312, 0.226988]]
b = f(a)
print(np.round(b, 5))

Output:

[[-0.1646   0.17897]
 [-0.14197  0.22699]]

And just like that, the function worked exactly as I expected.

More Practice

Once I figured out this trick, I started experimenting with more transformations. Here are a few examples that I found really useful.

Leaky ReLU (used in deep learning)

def leaky_relu(arr, alpha=0.01):
    arr = np.array(arr)
    return np.where(arr > 0, arr, alpha * arr)

print(leaky_relu([[-2, -0.5, 0, 1, 3]]))

This keeps positive numbers the same but scales negative numbers down by a factor.

Custom Transformation

For fun, I tried squaring positive numbers and applying a logarithm to negatives:

def custom_transform(arr):
    arr = np.array(arr)
    return np.where(arr > 0, arr**2, np.log1p(-arr))

print(custom_transform([[-0.5, 0.2, 1.5]]))

Vectorizing a Lambda Function

If you still prefer writing lambdas, NumPy has np.vectorize to apply them element-wise:

f_vec = np.vectorize(lambda x: x if x > 0 else np.exp(x) - 1)
print(f_vec(a))

This gives the same result as before, though np.where is usually faster.

Final Thought

Running into a TypeError was frustrating at first, but it turned out to be a great learning moment. I realized that Python lists and NumPy arrays behave very differently, especially when it comes to element-wise operations. The key takeaway for me was to always convert lists into NumPy arrays and to use tools like np.where or np.vectorize to keep code clean and error-free. Fixing this one issue not only solved my problem but also gave me the foundation to build custom transformations I now use in data analysis, machine learning, and other projects.

Related blog posts