Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Functional Programming Map, Filter, and Reduce Functions in Python

Learn how to harness the power of functional programming in Python using map, filter, and reduce functions. Master these essential concepts and take your Python skills to the next level. …


Updated June 3, 2023

Learn how to harness the power of functional programming in Python using map, filter, and reduce functions. Master these essential concepts and take your Python skills to the next level.

What are Functional Programming Map, Filter, and Reduce Functions?

Functional programming is a programming paradigm that emphasizes the use of pure functions, immutability, and the avoidance of changing state. In Python, we can utilize map, filter, and reduce functions to achieve functional programming goals.

Definition:

  • Map: Applies a given function to each item of an iterable (such as a list or tuple) and returns a new iterable with the results.
  • Filter: Constructs an iterator from elements of an iterable for which a function returns True. It filters out elements that do not satisfy the condition specified by the function.
  • Reduce: Applies a given function of two arguments cumulatively to the items of an iterable, from left to right, so as to reduce the iterable to a single output.

Step-by-Step Explanation

Let’s explore each function with a step-by-step breakdown:

Map Function

The map function is used when you need to perform an operation on each item in an iterable. Here’s how it works:

  • Create a list of numbers: [1, 2, 3, 4, 5]
  • Define a function that takes one argument (in this case, x = lambda x: x * 2)
  • Pass the function to the map method and apply it to each item in the list
numbers = [1, 2, 3, 4, 5]
double_numbers = list(map(lambda x: x * 2, numbers))
print(double_numbers)  # Output: [2, 4, 6, 8, 10]

Filter Function

The filter function is used when you need to select only the items that meet a certain condition. Here’s how it works:

  • Create a list of numbers: [1, 2, 3, 4, 5]
  • Define a function that returns True if the number is even (in this case, x = lambda x: x % 2 == 0)
  • Pass the function to the filter method and apply it to each item in the list
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4]

Reduce Function

The reduce function is used when you need to perform an operation on each item in an iterable and combine the results. Here’s how it works:

  • Create a list of numbers: [1, 2, 3, 4, 5]
  • Define a function that takes two arguments (in this case, x = lambda x, y: x + y)
  • Pass the function to the reduce method and apply it to each item in the list
numbers = [1, 2, 3, 4, 5]
sum_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_numbers)  # Output: 15

Code Explanation

In the above code examples:

  • map() function applies a given function to each item of an iterable and returns a new iterable with the results.
  • filter() function constructs an iterator from elements of an iterable for which a function returns True. It filters out elements that do not satisfy the condition specified by the function.
  • reduce() function applies a given function of two arguments cumulatively to the items of an iterable, from left to right, so as to reduce the iterable to a single output.

Conclusion

In this tutorial, we have learned how to use map, filter, and reduce functions in Python. These functions are essential tools for functional programming and can help you write more efficient and concise code. Practice using these functions with different data structures and scenarios to become proficient in their usage.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp