# Higher Order Functions in Python : map, filter, sorted, reduce

**Higher order functions (HOFs)** in Python are basically those functions that take one or multiple functions as an argument or they return a function as its result. In Python, we have two different choices to create higher-order functions.

- We can use nested scopes to create higher-order functions
- We can also use callable objects to create the same

Using higher-order functions in code the overall code becomes simpler and shorter.

We have some built-in higher-order functions in Python.

## Map Function

**Syntax :- map (function, sequence)**

It takes a function and sequence as the arguments and then returns a list after applying the function to each sequence’s items. Let us consider an example :-

#function for performing the multiplication operation def multiply(n): #retuning the values return n*n #driver code to test the program num = (10,15,78,25) #map function(function, sequence/iterables) result = map(multiply, num) #returning the result in a list print(list(result))

Output : [100, 225, 6084, 625]

## Filter Function

**Syntax :- ****filter (function, sequence/iterable)**

Filter takes two parameters, the first one is a function and the second one is a sequence. Then it returns an iterator that passed through a function test for those elements function returns True. Let us consider an example, suppose we want to filter out those numbers which are greater than 50 from a given list of numbers.

#driver code to test the program numbers = [55, 92, 27, 48, 34, 62, 71, 18, 28, 43] #function to check the numbers whether they are greater than 50 or not def numCheck(n): if n < 50: return False else: return True #passing through the filter function result = filter(numCheck, numbers) #displaying the result for n in result: print(n)

Output : 55 92 62 71

## Sorted Function

The sorted function simply sorts a given sequence in a specific order. (either in ascending order or in descending order)

**Syntax :- sorted (sequence, key (optional), reverse(optional))**

**Note :-** There is a basic difference between sort() and sorted() function in Python. The difference is that the sort() function doesn’t return any value but in the case of sorted() function, it returns an iterable list. Let us take an example :

#declaring string str = "codespeedy" #calling sorted function result = sorted(str) # sorting string #displaying result print(result)

Output : ['c', 'd', 'd', 'e', 'e', 'e', 'o', 'p', 's', 'y']

By default, it takes an ascending order. To print in descending we can use the ‘reverse = True’ parameter.

#declaring string str = "codespeedy" #calling sorted function with reverse=True result = sorted(str,reverse=True) # sorting string #displaying result print(result)

Output : ['y', 's', 'p', 'o', 'e', 'e', 'e', 'd', 'd', 'c']

Let us also consider an example with the key parameter

#take the second element for sort def sort(x): return x[1] #random list random = [(2, 5), (4, 6), (7, 2), (1, 9)] #sort list with key result = sorted(random, key = sort) #print list print('Sorted list:', result)

Output : Sorted list: [(7, 2), (2, 5), (4, 6), (1, 9)]

## Reduce Function

**Syntax :- reduce (function, sequence/iterable)**

It takes a sequence/iterable of input data then applies to the function provided and gives a single result. For example, if we want to calculate the addition for a given list of integer numbers we can implement this code –

#import the reduce function from functools module from functools import reduce #function to divide two numbers def addition(a,b): return a+b #taking input n=map(int, input("Enter the numbers you want to add: ").split()) #reduce function reduce(addition,n)

In this code, the addition function will take two values so reduce will take the first two values from the iterable itself and then it will take one value at a time from the list n using the previous call’s return value.

Also, read: Map, Reduce and Filter Operations in Python

## Leave a Reply