Python

 

 

Python provides three powerful functions: map(), filter(), and reduce() that help you apply functional programming concepts to solve problems in an efficient and concise way. These functions are widely used for transforming, filtering, and reducing data in Python.

Objective:

The objective of this tutorial is to understand how to use the map(), filter(), and reduce() functions in Python with practical examples. We will explore their syntax and see how they can be applied to real-world problems to make your code more readable and efficient.

Code Example:

# Using map() to square each element in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print("Squared numbers:", squared_numbers)

# Using filter() to get even numbers from a list
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)

# Using reduce() to calculate the product of numbers in a list
from functools import reduce
product_of_numbers = reduce(lambda x, y: x * y, numbers)
print("Product of numbers:", product_of_numbers)

Explanation:

The program demonstrates how to use the three functions in Python:

  • map(): This function applies the given function to each item in the input iterable and returns a map object (which can be converted to a list or other iterables). In our example, it squares each number in the list.
  • filter(): This function filters the elements of the iterable based on a condition. It returns an iterator with the elements that meet the condition. In this case, it filters out only the even numbers.
  • reduce(): This function is used to apply a rolling computation to sequential pairs of values in the iterable. In the example, we use it to calculate the product of all numbers in the list.

How to Run the Program:

To run the program:

  1. Copy the code provided above into a Python file (e.g., functions_example.py).
  2. Ensure that you have Python installed on your system.
  3. Run the Python file using the command python functions_example.py in your terminal or command prompt.
  4. The program will output the results of the map(), filter(), and reduce() functions.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)