Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

In-built Functions (a.k.

a Higher order Functions)

A Higher Order function is one that takes a function as its argument.

Map
Map applies a unary function to each element in the sequence and returns a new sequence
containing the results, in the same order:
Map() is built-in function. map(function, iterable)
The map function takes a reference to a function as its first argument.
The map() function takes a function and applies it to each element in an iterable, such as a list
or tuple. The result is a new iterable containing the transformed values.
Examples:
from math import sqrt
map(sqrt, [1, 4, 9, 16]) # ==> [1.0, 2.0, 3.0, 4.0]

map(str.lower, ['A', 'b', 'C']) # ==> ['a', 'b', 'c']

numbers = [1, 2, 3, 4, 5]
doubled_numbers = list(map(lambda x: x * 2, numbers))
print(doubled_numbers)
[2, 4, 6, 8, 10]

strings = ["1", "2", "3", "4", "5"]


numbers = list(map(int, strings))
print(numbers)
[1, 2, 3, 4, 5]
Filter
Another sequence operation is filter, which tests each element with a unary predicate.
Elements that satisfy the predicate are kept; those that don’t are removed. A new list is
returned; filter doesn’t modify its input list.
The filter() function takes a function and an iterable and returns a new iterable containing
only the elements for which the function returns True. Here's the basic syntax:
filter(function, iterable)

Example:
filter(str.isalpha, ['x', 'y', '2', '3', 'a']) # ==> ['x', 'y', 'a']
Suppose you have a list of numbers and you want to filter out the even ones:
numbers = [1, 2, 3, 4, 5]
odds = list(filter(lambda x: x % 2 != 0, numbers))
print(odds)
[1, 3, 5]
In this example, we use a lambda function to define the filter condition. The function
returns True for odd numbers and False for even numbers. The filter() function returns an
iterable object, so we use the list() function to convert it back into a list.

Example of using filter() is to remove empty strings from a list:


strings = ["hello", "", "world", "", "python"]
non_empty = list(filter(lambda x: len(x) > 0, strings))
print(non_empty)
['hello', 'world', 'python']
In this example, we use a lambda function to check the length of each string. The function
returns True for strings with a length greater than zero and False for empty strings.
Reduce
Reduce, combines the elements of the sequence together, using a binary function. In addition
to the function and the list, it also takes an initial value that initializes the reduction, and that
ends up being the return value if the list is empty.
reduce(function, iterable, init) combines the elements of the iterable/list from left to right.
The reduce() function takes a function and an iterable and applies the function to the first two
elements, then to the result and the next element, and so on, until all elements have been
processed. The result is a single value
The reduce() function is part of the functools module, so you need to import it before using it:
from functools import reduce

Examples:
reduce(max, [5, 8, 3, 1]) # ==> 8

For example, we can use reduce to glue together a sequence into a string:
reduce(lambda s,x: s+str(x), [1, 2, 3, 4], '') # ==> '1234'

Let’s look at an example. Suppose you have a list of numbers and you want to calculate their
sum using reduce():
from functools import reduce
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total)
15
In this example, we use a lambda function to define the operation we want to apply to each
pair of elements. The function takes two arguments (x and y) and returns their sum.
The reduce() function applies this function to the first two elements (1 and 2), then to the
result (3) and the next element (3), then to the result (6) and the next element (4), and so on,
until all elements have been processed.

You might also like