Functional Programming in Python

Harnessing the Power of Lambdas and Higher-Order Functions

Learn how to write concise and powerful code in Python using functional programming techniques. This tutorial covers functions, lambda expressions, and higher-order functions such as map, filter, and reduce.

Programming
Author
Affiliation
Published

February 5, 2024

Modified

March 11, 2025

Keywords

Python functional programming, lambda functions, map filter reduce, higher-order functions, functional programming in Python

Introduction

Functional programming is a paradigm that treats functions as first-class citizens, meaning that functions can be assigned to variables, passed as arguments, and returned from other functions. In Python, you can write concise and expressive code using lambda expressions and higher-order functions such as map, filter, and reduce. This tutorial will explain these concepts and demonstrate how they can be used to write more elegant and efficient code.



Core Concepts

First-Class Functions

In Python, functions are first-class objects. This means you can: - Assign functions to variables. - Pass functions as arguments to other functions. - Return functions from other functions.

Lambda Functions

Lambda functions are anonymous functions defined with the lambda keyword. They are typically used for short, throwaway functions that are not complex enough to warrant a full function definition.

Higher-Order Functions

Higher-order functions are functions that take other functions as arguments or return them as results. Python provides several built-in higher-order functions:
- map(): Applies a function to every item of an iterable.
- filter(): Filters items out of an iterable based on a condition.
- reduce(): Applies a function cumulatively to the items of an iterable (available in the functools module).

Examples: Python vs. R

Below, you will see examples demonstrating functional programming concepts in Python, with an R comparison for context.

# Python: Lambda Function and Higher-Order Functions

# Using a lambda to double a number
double = lambda x: x * 2
print("Double of 5 is:", double(5))

# Using map() with a lambda to double each element in a list
numbers = [1, 2, 3, 4, 5]
doubled = list(map(lambda x: x * 2, numbers))
print("Doubled list:", doubled)

# Using filter() with a lambda to select even numbers
evens = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", evens)

# Using reduce() to compute the product of a list of numbers
from functools import reduce
product = reduce(lambda a, b: a * b, numbers)
print("Product of numbers:", product)
# R: Anonymous Functions and Higher-Order Functions

# Using an anonymous function with lapply to double numbers
numbers <- c(1, 2, 3, 4, 5)
doubled <- lapply(numbers, function(x) x * 2)
print("Doubled list:")
print(unlist(doubled))

# Using Filter() with an anonymous function to select even numbers
evens <- Filter(function(x) x %% 2 == 0, numbers)
print("Even numbers:")
print(evens)

# Using Reduce() to compute the product of numbers
product <- Reduce(function(a, b) a * b, numbers)
print("Product of numbers:")
print(product)

Conclusion

Functional programming in Python allows you to write code that is concise, expressive, and efficient. By leveraging first-class functions, lambda expressions, and higher-order functions like map, filter, and reduce, you can perform complex operations in a clear and compact manner. Experiment with these concepts to see how they can simplify your coding tasks and improve code readability.

Further Reading

Happy coding, and enjoy exploring the power of functional programming in Python!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Functional {Programming} in {Python}},
  date = {2024-02-05},
  url = {https://www.datanovia.com/learn/programming/python/advanced/functional-programming.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Functional Programming in Python.” February 5, 2024. https://www.datanovia.com/learn/programming/python/advanced/functional-programming.html.