Master Python Higher-Order Functions: Real-World Examples & Tips
Learn what higher-order functions are in Python, how they can accept or return other functions, explore built-in examples like map, filter, sorted, reduce, and see custom implementations and lambda integrations through clear code snippets and practical usage tips.
What is a Higher-Order Function?
In Python, a higher‑order function is a function that either takes one or more functions as arguments, returns a function as its result, or both.
Example 1: Passing a Function as an Argument
def apply_func(fn, x):
return fn(x)
def square(n):
return n * n
result = apply_func(square, 5)
print(result) # Output: 25Here apply_func receives another function fn (e.g., square) and applies it to a value.
Example 2: Returning a Function
def make_multiplier(n):
def multiplier(x):
return x * n
return multiplier
double = make_multiplier(2)
triple = make_multiplier(3)
print(double(5)) # Output: 10
print(triple(5)) # Output: 15 make_multiplierreturns a new function, making it a higher‑order function.
Common Built‑In Higher‑Order Functions
map
numbers = [1, 2, 3, 4]
squared = map(lambda x: x ** 2, numbers)
print(list(squared)) # Output: [1, 4, 9, 16]filter
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4, 6]sorted
names = ["Alice", "Bob", "Charlie"]
sorted_names = sorted(names, key=lambda name: len(name))
print(sorted_names) # Output: ['Bob', 'Alice', 'Charlie']reduce
from functools import reduce
numbers = [1, 2, 3, 4]
sum_result = reduce(lambda x, y: x + y, numbers)
print(sum_result) # Output: 10Creating Your Own Higher‑Order Functions
def compute(operation):
def calculator(a, b):
return operation(a, b)
return calculator
add = compute(lambda a, b: a + b)
multiply = compute(lambda a, b: a * b)
print(add(3, 4)) # Output: 7
print(multiply(3, 4)) # Output: 12Combining lambda with Higher‑Order Functions
# Square roots of numbers
import math
nums = [4, 9, 16, 25]
roots = list(map(lambda x: math.sqrt(x), nums))
print(roots) # Output: [2.0, 3.0, 4.0, 5.0]
# Numbers greater than 10
greater_than_10 = list(filter(lambda x: x > 10, nums))
print(greater_than_10) # Output: []Benefits of Using Higher‑Order Functions
They lead to more concise, reusable, and expressive code, reduce boilerplate loops, and integrate naturally with functional programming tools such as itertools and functools.
Further Learning Suggestions
Explore advanced functional concepts like currying and partial application.
Combine itertools and functools for powerful data processing.
Learn about decorators, which are also higher‑order functions.
Apply higher‑order functions in real projects to replace traditional loops.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
