Higher Order Functions in Python

Last Updated : 1 Jul, 2026

A Higher-Order Function (HOF) is a function that works with other functions. It can accept a function as an argument, return a function or do both. This allows functions to be reused and combined in flexible ways.

Python
def process(func):
    return func("python")

def to_upper(text):
    return text.upper()

print(process(to_upper))

Output
PYTHON

Explanation:

  • process() is a higher-order function because it accepts another function as an argument.
  • to_upper() takes a string and converts it to uppercase.
  • process(to_upper) passes "python" to to_upper() and returned result is "PYTHON".

Examples

Example 1: In this example, a function is passed as an argument to another function. The higher-order function uses the received function to process the given value.

Python
def apply(func, x): 
    return func(x)

def square(n):
    return n * n

print(apply(square, 5))

Output
25

Explanation:

  • apply(func, x) accepts a function and a value as arguments.
  • square(n) returns the square of the given number.
  • apply(square, 5) calls square(5).

Example 2: In this example, a higher-order function returns another function. The returned function remembers the value passed to the outer function and uses it later when called.

Python
def multiplier(n): 
    return lambda x: x * n

double = multiplier(2)
triple = multiplier(3)

print(double(5))
print(triple(5))

Output
10
15

Explanation:

  • multiplier(n) returns a lambda function that multiplies its input by n.
  • double stores a function that multiplies numbers by 2.
  • triple stores a function that multiplies numbers by 3.
  • double(5) returns 10 and triple(5) returns 15.

Built in Higher Order Functions

Python provides several built-in higher-order functions that accept other functions as arguments. These functions make it easier to transform, filter and organize data without writing explicit loops.

1. map(): applies a given function to every element in an iterable and returns the transformed values.

Python
a = [1, 2, 3, 4]
res = list(map(lambda x: x ** 2,a))
print(res)

Output
[1, 4, 9, 16]

Explanation: map(func, iterable) applies func to each element in iterable and lambda x: x ** 2 squares each number in the list.

2. filter(): selects elements from an iterable based on a condition and returns only those that satisfy it.

Python
a = [1, 2, 3, 4, 5, 6]
res = list(filter(lambda x: x % 2 == 0, a))
print(res)

Output
[2, 4, 6]

Explanation: filter(func, iterable) applies func to filter elements satisfying the condition and lambda x: x % 2 == 0 retains only even numbers.

3. sorted(): use a key function to determine how elements should be ordered.

Python
a = ["python", "java", "javascript"]
res = sorted(a, key=len)
print(res)

Output
['java', 'python', 'javascript']

Explanation: sorted(iterable, key=func) sorts based on func applied to each element and key=len sorts the strings by length.

Applications of Higher order functions

Higher-order functions are commonly used in concepts such as closures and decorators. These techniques help create reusable, flexible and maintainable code by allowing functions to work with other functions.

1. Using closure: A closure is created when a function returns another function that continues to access variables from the outer function even after the outer function has finished executing. This allows data to be preserved between function calls.

Python
def create_counter(start):
    count = start

    def increment():
        nonlocal count
        count += 1
        return count

    return increment

counter = create_counter(5)
print(counter())
print(counter())
print(counter())

Output
6
7
8

Explanation:

  • create_counter() returns the inner increment() function.
  • increment() remembers the value of count even after create_counter() finishes execution.
  • nonlocal count allows the inner function to modify the outer variable.
  • Each call to counter() updates and returns the stored value.

2. Using decorator: Decorators are functions that extend the behavior of other functions without modifying their original code. They are widely used for logging, validation, authentication and performance monitoring.

Python
def display_message(func):

    def wrapper():
        print("Starting task...")
        func()
        print("Task completed.")

    return wrapper

@display_message
def greet():
    print("Welcome!")

greet()

Output
Starting task...
Welcome!
Task completed.

Explanation:

  • display_message() is a decorator that accepts a function as an argument.
  • wrapper() adds extra behavior before and after the original function call.
  • @display_message applies the decorator to greet().
  • Calling greet() executes wrapper(), which in turn calls the original function.
Comment