Map & Filter
In this note, we will be covering some useful built-in functions to help us extend our list comprehension capabilities.
These two functions that we learn will also be revisited when we do functional programming and lambda functions.
The Map Function
The idea of a map function is to apply a function to an iterable data.
One thing to note about the map function is that it doesn’t return a specific data type, but rather, an python iterable data. Therefore, after we apply the map function, we just execute a list function on it.
In example 2, we are doing a lot of unnecessary work to make our original word variable uppercased. This is an example of how you shouldn’t use map() for every little changes you want to a string.
This also applies to all data structure that has methods. You don’t want to use methods with map, since there is a high probablity that there is already method for what you might want to do.
Filter Function
The idea of the filter function is to filter out items from a data set that meets a certain condition.
It is true that this can be done differently, but this was a simplistic use of filter to show we can filter out variables that satisfies condition… aka the condition is True.
Example Problem: List of Palindromic Numbers
Our goal in this example program is to create a list of palindromic numbers (numbers that are palindromes) from 1 to 10,000.
Explanation
Composition of Functions
Composition of Functions is the idea of using functions within a function call or apply one function to the result of another function. You can read more about them here.
Last updated