Map, filter, reduce in python
Map, filter, and reduce are all three powerful built-in functions in Python programming. These three functions are commonly used in Python to apply operations to repetitions (such as lists or tuples) in functional programming styles. These functions allow Python programmers to apply functions to list elements in sequence, custom filter lists or tuple elements based on a program condition, and collect lists or tuple elements into a single value.
Python map function.
The map function in a Python program applies a given user function to each item/element of a repetition (such as a list element), and returns/displays a repetition value in the program.
Python map function syntax.
map(function, iterator)
Map function – map is a built-in Python function that applies an operation to each list element of a repetition.
Iterator – It passes a repetition of a particular data type (e.g., list or tuple) to a function element by element.
Python example: map function to square integers in a list.
# let use map function to squareroot every number
integer = [1, 2, 3, 4, 5, 6, 7 ,8]
squart_integer = list(map(lambda p: p ** 2, integer))
print(“\n the map sqrt is -“, squart_integer) # the result is – [1, 4, 9, 16, 25, 36, 49, 64]
In the above example.
lambda p: p ** 2 is an anonymous lambda function defined to display the square root of all integers of its program input p.
map(lambda p: p ** 2, integer) This lambda function is applied to each element in a list of integers, resulting in the output [1, 4, 9, 16, 25, 36, 49, 64].
Python filter function.
The built-in filter function in Python programming creates a repetition object from the elements of a repetition list or tuple. The function displays the True result in the processed value.
Python filter function syntax.
filter(function, iterable)
Filter function – Filter is a Python function that previews the result in the program execution as True or False value.
Function iterable – It is a filter element iterable (e.g., list, or tuple) whose stored elements will be manually filtered.
Python filter function example: Filter and display odd numbers in a list.
integer = [2, 3, 6, 7, 11, 12, 15, 17, 18, 19, 20]
# let tray filter function to findout odd numbers
odd_integer = list(filter(lambda p: p % 2 == 1, integer))
print(“\n the odd integer is -“,odd_integer) # the result is – [3, 7, 11, 15, 17, 19]
In the above program example.
lambda p: p % 2 == 1 An anonymous lambda function is defined that displays the value True when the variable p is odd.
filter(lambda p: p % 2 == 1, integer) Here lambda function is applied to each list element in the integer list, and holds only those list elements for which the function returns True value, which results in odd integers [3, 7, 11, 15, 17, 19].
Python reduce function.
The reduce function is used in Python programming to repeatedly apply a function to all the elements in a list and reduce them to a single cumulative value.
Remember – In Python language version 3, reduce is a built-in function of the functools module, so you need to explicitly import this function before using it in any of your Python programs.
Python reduce function syntax.
from functools import reduce
reduce(function, iterator, initial)
reduce function – This is a two-argument function in Python that is applied cumulatively to the items of an iteration, moving from left to right, so that the iterator is reduced to a single value.
Function iterator – This is a reduce function iterator (e.g., list, or tuple) whose elements will be reduced in the function process.
initial (optional) – This is a start value (default is None) that will be used as the first argument for the first call to the reduce function.
Python reduce function example: Calculate the product of numbers in a list.
from functools import reduce
integer = [1, 2, 3, 4, 5, 6, 7]
# let tray reduce function to multiply all numbers in sequence
multiply = reduce(lambda p, q: p * q, integer)
print(“\n the given list numbers product/multiply is -“,multiply) # the result is – 5040
In the above reduce program example.
Here lambda p, q: p * q defines an anonymous function (lambda function) that counts the multiplication of two integers p and q.
reduce(lambda p, q: p * q, integer) applies the cumulative operation to all the elements of the list of numbers, resulting in the output 5040 (1 * 2 * 3 * 4 * 5 * 6 * 7).
When to use the trie functions in Python.
map function – The map function is used in Python programs when the Python programmer needs to apply the map function operation to each item in a list element iterable and preview the list element result.
filter function – The filter function is used in Python programs when you need to filter and display a particular list element from the iterable based on a program condition.
reduce function – Use the reduce function in Python when you need to add the elements of a list element iterable to a single value, especially when the order of list element operations is important (such as calculating a cumulative product or cumulative add).
Python map, filter, reduce function explanation.
Program efficiency – Using map, filter and reduce functions in Python programs. As functional programming creations can often contain more detailed expressions than traditional Python loops, this makes Python programs more readable and easier to compile.
Program readability – While custom lambda functions in Python programs can make program code more compact or smaller, Python programmers must carefully use lambda functions in the right places in Python programs to maintain the clarity and readability of the lambda function program code.