Python Map, Filter & Reduce Functions
Python Map Function
The map()
function in Python is a built-in function that applies a function to each element of an iterable (such as a list, tuple, or string) and returns a new iterator with the transformed elements.
The map()
function has the following syntax:
This is an example of a list transformation using the map()
function:
The
map()
function is a convenient way to apply a function to many elements in an iterable. It is often used to transform or filter the elements of a list or other iterable object.
Python Filter Function
The filter()
function in Python is a built-in function that takes an iterable and a function as input, and returns an iterator with the elements that return True when passed to the function.
The filter()
function has the following syntax:
This is an example of a list transformation using the filter()
function:
The filter() function is a convenient way to select specific elements from an iterable based on a certain condition. It is often used to filter lists or other iterable objects to create new lists with only the elements that satisfy a certain criterion.
Python Reduce Function
The reduce()
function in Python is a built-in function that applies a function to an iterable in a cumulative manner to reduce the iterable to a single value. It is a part of the functools module, so you need to import it before you can use it.
The reduce() function has the following syntax:
The reduce()
function is a function that takes in two arguments and returns a single value. The 'iterable'
is an iterable
object, such as a list, tuple, or string. The initializer
is an optional argument that specifies an initial value to be used in the reduction. If the initializer
is not provided, the reduce()
function will use the first element of the iterable
as the initial value.
This is an example of a list transformation using the reduce()
function:
The
reduce()
function is a useful tool for performing a cumulative operation on an iterable object. It can be used to perform a wide variety of tasks, such as calculating the sum or product of a list of numbers, finding the longest string in a list, or concatenating a list of strings.
Last updated