HOWTO · NumPy

How to Map a Function in NumPy

Apply a Python function to every value in a NumPy array with numpy.vectorize() or map() and a lambda. See how each approach works and when to use it.

On this page

To apply a scalar Python function to each value in a NumPy array, use numpy.vectorize() to wrap the function or use Python’s map() with a lambda and convert the result to an array. Both approaches are useful when you want to reuse scalar logic; neither should be confused with NumPy’s native array operations, which are generally the better fit when an equivalent operation is available.

Use numpy.vectorize() to apply a function element-wise

numpy.vectorize() adapts a function that accepts one value at a time so you can call it with an array. It applies the wrapped function element by element and returns an array of results. This is convenient when existing Python logic is written for a scalar and you want to use it with array data.

The example wraps a scalar function that squares its input, then applies it to five integers:

import numpy as np

def square(value):
    return value ** 2

values = np.array([1, 2, 3, 4, 5])
square_each = np.vectorize(square)
result = square_each(values)

print(result)

Here, square remains an ordinary Python function; np.vectorize() provides an array-oriented calling interface. It is a convenience wrapper, not a performance optimization: it does not turn the function into a compiled NumPy operation. For arithmetic that NumPy already supports, an expression such as values ** 2 is simpler and uses NumPy’s array operation directly. Use numpy.vectorize() when adapting scalar logic makes the code clearer or easier to reuse.

Use map() with a lambda function

Python’s built-in map() calls a function once for each item in an iterable. A lambda is a compact way to define a small function inline. When the input is a NumPy array, map() produces an iterator of mapped values; wrap that iterator with list() and pass the resulting list to np.array() to create an array.

This example uses a lambda to square the same values, then converts the mapped values into a NumPy array:

import numpy as np

values = np.array([1, 2, 3, 4, 5])
mapped_values = map(lambda value: value ** 2, values)
result = np.array(list(mapped_values))

print(result)

The lambda receives each array value separately, and map() yields each returned square in sequence. Converting the iterator to a list consumes it; np.array() then builds an array from those values. This approach is handy for a short scalar operation that belongs next to the mapping call. For longer logic, define a named function so it is easier to read and maintain, and pass that function to map() in place of the lambda.

Choose numpy.vectorize() when you want an array-shaped interface around an existing scalar function. Choose map() with a lambda for a small inline transformation, especially when the resulting iterator is useful before conversion. For operations such as squaring every value, prefer the direct NumPy expression values ** 2 when it expresses the intended calculation. These choices describe different ways to apply Python logic; they do not establish that one mapping approach is faster than another.