Python 09

Higher Order Functions

Python has a higher order functions, and we will go through .map() and .filter() in this post.

We call higher order function which can take another function as a parameter or argument. In higher order function, we can return a function as a result of another function.

.map()

.map() is a function that works as an iterator. It returns a new list after applying a function to every item in the list.

'''
From the previous post
>>> double([1, 2, 3])
2, 4, 6
'''

Do you remember exercises that we did in previous post? The function double() takes a list of numbers and returns doubled numbers.

There is more simple way of doing this with .map()

def double_number(number: int) -> int:
  return number * 2

print(list(map(double_number, [1, 2, 3])))

.map() takes two argument, one is a function and another one is list in this case.

Also we can refactor this with lambda

print(list(map(lambda number: number * 2, [1, 2, 3])))

# To return squared value, we can use **
print(list(map(lambda number: number ** 2, [1, 2, 3])))

.filter()

.filter() is a method that tests each element in the given object to be true or not with the given function.

For example, if we want to get even numbers from the list, we can use filter.

numbers =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

# Modulo operator
# it divides by given number and returns remainder

# how to check for EVEN number
# 2 % 2 == 0
# 4 % 2 == 0
# 6 % 2 == 0

print(list(filter(lambda number: number % 2 == 0, numbers)))

We reused structure above, with the filter function. This time, we passed numbers list, not the list itself.

List Comprehensions

Can you believe that their is more efficient way to doing this? Well, that is the time for list comprehensions.

First, let’s take a look for the example of a list comprehension.

numbers =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

[number for number in numbers if number % 2 ==0]

Wow, what is going on here? To break down, we have to sperate this to three parts.

The first part is returning list number The second part is a loop for number in number The third part is a condition if number % 2 == 0

And when we print the list comprehension above, we will get the same result with print(list(filter(lambda number: number % 2 == 0, numbers)))

# filter and return only EVEN numbers
numbers =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

print([number for number in numbers if number % 2 == 0])

# result
[2, 4, 6, 8, 10]

Now it is time for .map()

# map - double the numbers
print([number * 2 for number in numbers])

It is much simpler than using .map()!

❗ Review for list comprehension

  1. The first argument in list comprehension asks how should we build the list.
  2. The second argument is the loop part.
  3. The third argument is the conditional and it is optional.

Easy practice ❗ How do we get the odd numbers from the list

Toggle Answer πŸ‘‡
# get odd num
print([number for number in numbers if number % 2 != 0])
Tiny Tip

Using list comprehension is generally easier than using .map() and .filter()

Categories:

Updated:

Comments