Javatpoint Logo
Javatpoint Logo

map, filter, and reduce in Python with Examples

Python Streams

Python stream is a term for a particular paradigm for data processing that involves the sequential processing of data items as they pass through a pipeline of processes. Streams allow data processing to be continuous, effective, and memory-friendly without loading the entire dataset into memory at once.

The map, filter, and reduce functions in Python are higher-order functions that work on sequences of data, such as lists or other iterable objects. Streams can be used in conjunction with these methods. With the help of these functions, you may quickly and effectively analyze typical data on sequence elements.

What is functional programming?

Functional programming is a programming paradigm that treats computation as evaluating mathematical functions and avoids changing state and mutable data.

In functional programming, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from other functions.

Functional programming emphasizes immutability, meaning that once a value is assigned, it cannot be changed, and it avoids side effects, which are changes to the state or behaviour that affect the result of a function beyond its return value.

Functional programming is possible in Python using a number of features and tools, such as:

1. Higher-order functions - Python enables the assignment of functions to variables, the passing of functions as arguments, and the return of functions as values. Higher-order functions are the functions that accept other functions as arguments or return them as results and can be used as a result of this. Strong functional programming techniques like giving functions as parameters, returning functions from functions, and constructing functions on the fly are made possible by higher-order functions.

2. Lambda Functions - Lambda functions, also known as anonymous functions, can be defined inline without requiring a formal function declaration and are short and one-time-use functions. They are helpful for the performance of a single task that only requires one line of code to convey.

3. map, filter, and reduce - Map, Filter, and Reduce are built-in Python functions that can be used for functional programming tasks. With the help of these operations, you may apply a specific function to sequence items using the 'map', filter sequence elements based on a condition using the 'filter', and cumulatively aggregate elements using the 'reduce'.

4. List Comprehension - List comprehensions are supported by Python, which are simple and expressive techniques to build new lists from older ones. Functional programming techniques such as mapping, filtering, and aggregating can be implemented using list comprehensions.

Let's dive deeper into the map(), filter(), and reduce() functions in Python.

1. map() - Python's map() method applies a specified function to each item of an iterable (such as a list, tuple, or string) and then returns a new iterable containing the results.

The map() syntax is as follows: map(function, iterable)

The first argument passed to the map function is itself a function, and the second argument passed is an iterable (sequence of elements) such as a list, tuple, set, string, etc.

Example 1 - usage of the map():

Output:

1, 4, 9, 16, 25
Squares: [1, 4, 9, 16, 25]

Here, the map function takes each element one by one from the data starting from x = 1. Each element is passed to the lambda function, returning its square. And the returned value is stored in the map object (an iterable).

Example 2:

As we all know that the input value is always a string. In the case of single-value input, we can use the 'int' function to convert it into an integer and the 'float' function to convert it into a float. But for a stream of inputs, we can use the map() function to achieve the same.

Output:

Enter integer values: 24 5 6 84 2 6
Integer data = [24, 5, 6, 84, 2, 6]
Enter Floating values: 25.4 2.6 1.9 0.6 3 5
Float data = [25.4, 2.6, 1.9, 0.6, 3.0, 5.0]

Example 3: Passing multiple iterables

You are given two lists. One contains the base values, and the other contains the power value. You are supposed to find the x**y for each x in list one and y in list two.

Output:

Answer: [2, 16, 216, 4096, 100000]

As we know, the pow(x, y) takes two arguments and returns the x**y. In the above example, the map function takes one element from base, and one element from power in order and passes them to the pow function till all the elements have been processed. In the end, it returns a map object, which is then type-casted into a list and stored in the variable named 'answer'.

2. filter() - The filter() function in Python filters elements from an iterable based on a given condition or function and returns a new iterable with the filtered elements.

The syntax for the filter() is as follows: filter(function, iterable)

Here also, the first argument passed to the filter function is itself a function, and the second argument passed is an iterable (sequence of elements) such as a list, tuple, set, string, etc.

Example 1 - usage of the filter():

You are given a list of integers and should filter the even numbers from the list.

Output:

2 4
Evens = [2, 4]

Example 2: Filtering Perfect Squares

You are given a list of random integers and should filter the perfect squares from them. A perfect square is a number that can be expressed as the product of the same whole number.

Output:

Answer:  [0, 1, 4, 9, 16, 81, 36]

In the above example, we have a data list that contains some random integers. The 'isPerfectSqr' function returns True for perfect square numbers and False for others. The filter function filters out the numbers from the data, which is a perfect square, and returns an iterable that contains those perfect square numbers. In the end, we converted the result into a list and printed it into the console.

Example 3: Filter Out Names Starting with H

You are given a list containing names of persons and you should filter out the names starting with the letter 'H'. Below is the solution of the problem:

Output:

Method 1 result = ['Harsh', 'Harry', 'Hassi']
Method 2 result = ['Harsh', 'Harry', 'Hassi']

In the above example, the lambda or H_name function returns true for each x in names starting with letter H. The filter function filters all the names which satisfies the condition.

3. reduce() - In Python, reduce() is a built-in function that applies a given function to the elements of an iterable, reducing them to a single value.

The syntax for reduce() is as follows: reduce(function, iterable[, initializer])

The function argument is a function that takes two arguments and returns a single value. The first argument is the accumulated value, and the second argument is the current value from the iterable.

The iterable argument is the sequence of values to be reduced.

The optional initializer argument is used to provide an initial value for the accumulated result. If no initializer is specified, the first element of the iterable is used as the initial value.

Here's an example that demonstrates how to use reduce() to find the sum of a list of numbers:

Example 1:

You are given a list containing some integers and you should find the sum of all elements in the list using reduce function. Below is the solution of the problem:

Output:

Sum of the integers of num_list : 55
Sum of the integers of num_list with initial value 10 : 65

In the above example, the reduce function takes two elements 1 and 2 from the num_list and passes to the add function in the first iteration. The add function returns the some of the 1 and 2 which is 3. In the second iteration, the reduce function passes the result of the previous call which is 3 and the next element which is also 3. This process is repeated until all elements have been processed.

In the case, where we pass the initial value (10), the reduce function takes one element from the num_list and initial value (10) and passes to the add function in the first iteration.

Example 2: Using operator functions with reduce function

In the below example, we have used operator.add to perform addition, operator.mul to perform multiplication and operator.concat to perform concatenation on strings.

Output:

Sum of all elements in my_list1 : 15
Product of all elements in my_list1 : 120
Concatenated string by using operator.concat : ILoveJavatpoint
Concatenated string by using operator.add : ILoveJavatpoint

Here, the function argument in the reduce function is replaced with the operator functions. All the steps are same as previous examples.

Example 3: Find the largest of the given numbers.

In this example, you are given a list of integers and you should find the largest number using the reduce function. Below is the solution of the problem:

The are three methods, we can use to achieve the same result:

Method 1 - Using normal function

Method 2 - Using Lambda function

Method 3 - Using max function

Output:

Largest found with method 1: 88
Largest found with method 2: 88
Largest found with method 3: 88

In the above example, the large function, lambda function, and the max function returns the maximum of x and y to the reduce function. And the reduce function parse all the elements one by one. At the end, it returns the largest of all.

CONCLUSION:

In conclusion, map(), filter(), and reduce() are built-in functions in Python that are commonly used for functional programming.

  • map() is used to apply a given function to each element of an iterable and returns a new iterable with the results.
  • filter() is used to filter elements from an iterable based on a given condition or function and returns a new iterable with the filtered elements.
  • reduce() is used to apply a given function to the elements of an iterable in a cumulative way, reducing the iterable to a single value.

These functions or tools give you strong capabilities for processing data quickly and expressively, making it simple to convert, filter, and aggregate data. They are frequently used in Python's functional programming concepts to create readable and efficient codes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA