The objective of this MicroPython Tutorial 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.
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.
1 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.
1 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.
1 3>5 2 7>5
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.
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.
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).
1 testList = [1,2,3,4,5,6,7,8,9,10] 2 finalList =  3 4 filterLambda = lambda x: x>5 5 6 for element in testList: 7 if filterLambda(element): 8 finalList.append(element) 9
10 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.
1 def isGreater(x):
2 return x>5
Now we simply call the filter function but instead of passing the lambda as first argument, we pass our named function.
The Future of Microprocessors Description: RISC-V, ARM, and x86 microprocessors are all competing to shape the future of computing. In this article, we explore the differences between these three architectures, their advantages and disadvantages, and how they may develop in the future.
This article will delve into four mainstream series of ultrasonic sensors: URM, HC SR04, Devantech, and Maxbotix, analyzing their working principles, performance characteristics, and real-world application examples.
Learn about HiFive Unmatched and VisionFive 2, two high-performance RISC-V CPU development boards. Explore their technical features, customization options, and broad application potential in IoT, AI, and embedded systems.