0

$USD

$USD
TUTORIALS ESP32ESP8266MicroPython

ESP32 / ESP8266 MicroPython Tutorial: Applying map function to lists

DFRobot Aug 29 2017 1871

The objective of this MicroPython Tutorial is to explain how to use the map function to perform operations over MicroPython lists. 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.

Introduction

The objective of this MicroPython Tutorial is to explain how to use the map function to perform operations over MicroPython lists. You can read more about lists and how to use them on this previous post.

Map is a function that receives as input both a function and an iterable (such as a list) and applies that function to each element of the iterable. It returns an object with the result that can be used to create, for example, a list or a set. Just note that not only lists are iterables. You can check here more about iterables in Python.

Although we can define regular functions to use as input of the map function (by using the def construct), one very useful concept are lambdas or anonymous functions, which were covered on this previous post. As we will see, they can be used with map and make our code much easier to write and more compact.

In our example, we will apply the map function to a list of integers, in order to increment by one each element. This is just an example to get started, since this function can be use for much more complex operations.

The tests shown here were performed on both the ESP8266 and the ESP32. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The images shown in this tutorial were taken from the tests executed on the ESP8266. All the tests were performed on uPyCraft, a MicroPython IDE. You can learn more about uPyCraft in this previous post.

Using lambdas

As stated in the introductory section, lambdas are very useful to use with functions such as map, so we can define our mapping function locally, which leads to more compact and easy to write code.
We will first define our list of integers, to which we will apply the map operation.

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

Now we will call the map function, which receives as input both the mapping function (in this case, a lambda that we will define) and the previously created list. As shown in the previous post about lambdas, their syntax is as follows:

1 lambda args: expression

Since our lambda will receive just one argument (each value of the list, one by one) and increment it, it should, be defined as indicated bellow.
1 lambda x: x+1

So, putting it inside the map function and also passing as input the list, we should have the command bellow. Note that we are also printing the type of the map return value to show that is no longer a list but rather a map object, that we can latter convert back to a list.

1 mappedObject = map(lambda x: x+1, testList)
2 print(type(mappedObject))

So to convert it back to a list, we just need the to call the list function and pass as input the returned map object.

1 mappedList = list(mappedObject)
2 print(mappedList)

You can check bellow in figure 1 the expected result upon executing these commands. As can be seen, the returning object is of class map, rather than the original list class we have passed. Nonetheless, upon a conversion back to list, we obtain the expected result, which is a list with each value incremented.

Figure 1 – Executing the map operation with lambdas.

Since the map operation can be difficult to understand at first, bellow is an example of how we could achieve the same result with other approach. So, we basically define our lambda and then iterate every element of the initial list, apply the function and put it in another list. Note that we use the append method to add elements to the final list. You can read more about this and other list methods here.

1 testList = [1,2,3,4,5,6,7,8,9,10]
2 finalList = []
3 myLambda = lambda x: x+1

5 for element in testList:
6   finalList.append( myLambda(element) )
7
8 print(finalList)

As can be seen, this is much more verbose and less elegant approach. Nonetheless, as shown in figure 2, it gives the same result as before.

Figure 2 – Alternative implementation for achieving the map function results.

Using functions

To complete our example, we will check how we can use the map function with a regular named function, rather than a lambda.

So we will first define a function called increment that receives an input argument and returns its value incremented by 1.

1 def increment (x):

2 return x+1

Now, as we did before, we simply call the map function, but this time passing as input the increment function rather than a lambda.

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

2 resultList = list(map(increment, testList))

3

4 print (resultList)

As can be seen in figure 3, the result is the same as before.

Figure 3 – Map list using a named function.

NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.