The objective of this post is to explain how to use the filter function with lists in MicroPython. This tutorial was tested both on the ESP32 and on the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.
The objective of this post is to explain how to use the filter function with lists in MicroPython.
The filter function receives as input a function and an iterable (in our case, a list) and applies the function to each element, moving to the output only the elements to which the passed function returns true.
So, it’s our responsibility to define the condition or conditions that result in true or false given an input, inside the passed function. In our simple example, our function will return true if a number is greater than 5 or false otherwise. So, given an initial list with multiple integers, we should end up with a list with just integers greater than 5.
A concept that is very useful for the filter function is the use of lambda or anonymous functions, which leads to a much more compact syntax. You can check more about lambdas on MicroPython on this previous post. You can also read more about lists and their methods here.
The tests shown here were performed on both the ESP8266 and the ESP32. The tests on the ESP32 were done using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The figures shown through this tutorial were taken from the tests on the ESP8266. All the tests were conducted on uPyCraft, a MicroPython IDE. You can learn more about the IDE in this previous post.
If you prefer you can check the video version at my Youtube Channel.
As stated before, lambdas play well with this type of functions and thus we will use them for this section. But first of all, we will declare our list of integers, to which we will apply the filter operation.
testList = [1,2,3,4,5,6,7,8,9,10]
Now we simply call the filter function, passing as input both the lambda function that implements the filtering condition and the list.
Taking into account the lambda syntax explained in the previous post, the code shown bellow corresponds to the lambda we need. Note that we have an input argument, which will be the current element of the list at each step, and after the “:” the expression that checks if the value is greater than the threshold of 5. You can use other threshold if you want.
lambda x: x>5
Note that the expression x>5 will result in a Boolean value (True or False), so we don’t need to use an IF. You can confirm it by sending the following commands on the MicroPython command line, which should return False and True, respectively.
The full filter function command can be seen bellow. Note that we are storing the output just to check that the output value is no longer a list but rather an object of class filter.
filteredResult = filter(lambda x: x>5 , testList) print (type(filteredResult))
We can simply convert it back to a list using the list function and passing as input the returned object. Note that we could have done this transformation in the same line we called the filter function, making this a one liner.
finalList = list(filteredResult) print(finalList)
You can check the expected result bellow, at figure 1. As can be seen, only the elements of the initial list that fulfill the filtering function condition are in the final result. At the beginning it is also included the example that shows the > operator doesn’t need an IF to give a Boolean result.
Figure 1 – Applying the filter function to a list using a lambda.
If we wanted to implement this operation resorting to a traditional loop, a possible solution is shown bellow. We simply iterate the whole list and if the current element fulfills the condition, we put it on the result. Although it works the same, the syntax is much more verbose and less elegant.
Note that in IF conditions, such as the one we have inside the loop, we don’t need to compare a Boolean value with True. Since the output of the lambda is already a Boolean, we don’t need to do if filterLambda(element) == True, we can simply do if filterLambda(element).
testList = [1,2,3,4,5,6,7,8,9,10] finalList =  filterLambda = lambda x: x>5 for element in testList: if filterLambda(element): finalList.append(element) print (finalList)
As can be seen in figure 2, the final result is the same as the one with the filter function approach.
Figure 2 – Alternative implementation to achieve the same results of the filter function.
Using named functions
For completion, we will also check how we can use the filter function with a named function, rather that with a lambda.
So we first define a function called isGreater, which receives as input the value that we want to check. If it is greater than 5, we return true, otherwise false. Again, we don’t need the IF in the comparison.
def isGreater(x): return x>5
Now we simply call the filter function but instead of passing the lambda as first argument, we pass our named function.
testList = [1,2,3,4,5,6,7,8,9,10] filteredList = list(filter(isGreater,testList)) print(filteredList)
As shown in figure 3, this returns the same result as before.
Figure 3 – Applying filter function to list with named function as input.