# Python Higher Order Functions Example

In one word, if a function can accept another function as input parameter, such a function is called a higher order function. Python provide several higher order functions to simplify your coding, this article will introduce them with examples.

### 1. map(function,interable…) Function.

The map function maps the specified sequence to the supplied function, that is to say, each element of the specified sequence will be executed with the provided function as the function input parameter.

```>>> # Use the map function to square the number values in the list
...
>>> # This function will return a number value that square the input number value.
... def square_value(number):
...     return number * number
...
>>> # The original number list.
... number_list = list(range(10))
>>>
>>> # Create new square number value list use map function. map function first parameter is the function name, the second parameter is the original number list.
... square_number_list = list(map(square_value,number_list ))
>>>
>>> # Print out the new square number list.
... print(square_number_list)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]```

### 2. reduce(function,interable) Function.

reduce function is used to accumulate the function result on each elements of a specified sequence. First, the first two elements of the sequence are executed by the specified function, then use the function result and the third element as the specified function’s input parameters to run the function again until all sequence elements has been evaluated.

reduce function is provide in functools library, so before using it, you should import it in your Python code like below.

`from functools import reduce`
```>>> from functools import reduce
>>>
...     return num_1 + num_2
...
>>> def multiple_number(num_1,num_2):
...     return num_1 * num_2
...
>>> number_list = list(range(1,6))
>>> print(number_list)
[1, 2, 3, 4, 5]
>>>
15
>>>
>>> reduce_multiple_number = reduce(multiple_number,number_list)
>>> print(reduce_multiple_number)
120```

### 3. filter(function,interable) Function.

The filter function is used to filter elements of a sequence. It takes two parameters, the first is a function and the second is a sequence. Each element of the sequence is passed to the function as a parameter for evaluation, and then True or False is returned. Finally, the element that returns True is placed in the new list.

```>>> # use the filter function to filter even numbers in the list
...
>>> # The original number list.
... number_list = list(range(10))
>>>
>>> def is_even(number):
...     return number % 2 == 0
...
>>> even_number_list = list(filter(is_even,number_list))
>>>
>>> print(even_number_list)
[0, 2, 4, 6, 8]
```

### 4. sorted(interable,key = function|reverse = False|True) Function.

Used to sort the provided sequences. key : is used to receive a comparison function to sort by. reverse : default value is False means sort sequence element from small to big, if set is’s value to True means sort sequence element from big to small.

#### 4.1 Sort numbers use sorted functions.

```>>> numbers = [100,2,7,-9,15,3]
>>> print(numbers)
[100, 2, 7, -9, 15, 3]
>>> sorted_numbers = sorted(numbers)
>>> print(sorted_numbers)
[-9, 2, 3, 7, 15, 100]

>>> sorted_numbers = sorted(numbers,reverse = True)
>>> print(sorted_numbers)
[100, 15, 7, 3, 2, -9]```

#### 4.2 Sort strings use sorted functions.

```>>> strings_list = ['jerry','Richard','Tom','kevin','jackie','miracle']
>>> print(strings_list)
['jerry', 'Richard', 'Tom', 'kevin', 'jackie', 'miracle']

>>> sorted_strings = sorted(strings_list)
>>> print(sorted_strings)
['Richard', 'Tom', 'jackie', 'jerry', 'kevin', 'miracle']

>>> sorted_strings= sorted(strings_list, reverse = True)
>>> print(sorted_strings)
['miracle', 'kevin', 'jerry', 'jackie', 'Tom', 'Richard']
>>>```

#### 4.3 sorted function usage with key parameter.

The sorted() function is also a higher-order function, which can also accept a key function to achieve custom sorting.  In below example, sorted function use Python built-in abs function as the key function to calculate each sequence element’s absolute value and then sort it.

```>>> numbers = [8,11,9,-6,-1,68,-100]
>>> print(numbers)
[8, 11, 9, -6, -1, 68, -100]
>>>
>>> sorted_numbers = sorted(numbers,key=abs)
>>> print(sorted_numbers)
[-1, -6, 8, 9, 11, 68, -100]
>>>
>>> sorted_numbers = sorted(numbers,key=abs,reverse=True)
>>> print(sorted_numbers)
[-100, 68, 11, 9, 8, -6, -1]```

This site uses Akismet to reduce spam. Learn how your comment data is processed.