The objective of this tutorial is to explain how to use 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 tutorial is to explain how to use lists in MicroPython. This tutorial was tested both on the ESP32 and on the ESP8266.
Lists are a very useful data structure from Python which are also available in MicroPython. We are not going to cover all the many available functionalities but rather taking a look at the main ones.
The tests were performed using uPyCraft, a MicroPython IDE. The easiest way to follow the tutorial is sending the commands in the MicroPython command line. You can check how to use it on uPyCraft in this previous post.
The tests were performed on both the ESP32 and the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The pictures shown through the tutorial are from the tests on the ESP32.
Creating a list in MicroPython is as simple as putting some elements separated by commas inside two square brackets. No need for calling constructors or any other function. Thus, with the code shown bellow we will create a list of integers.
intList = [1,2,3,4,5] print(intList)
Naturally, we can have lists of other types of objects. Bellow we are creating a list of strings.
stringList = ['a','b','c','d','e'] print(stringList)
In both cases, if we check the type of these variables with the type function, it should return list.
You can check bellow in figure 1 the output of these commands, in the Python command line of the uPyCraft IDE.
Figure 1 – Output of the commands to create a list of integers and a list of strings.
One interesting thing about these lists is that we can actually have elements of different types. In the example bellow we have a list that has strings, integers and even another list as elements.
objectList = [1, 2, 'a', 'b', 'c', [ 1, 2, 3] ] print (objectList) type(objectList)
You can check the output of these commands in figure 2.
Figure 2 – Output of the commands to create a list of objects.
Just as a note, for creating an empty list, we use the square brackets without putting any values inside them. We can then add elements latter.
emptyList =  print (emptyList)
Accessing lists elements
To access elements of a list, we can use square brackets, which is similar to the way we access array elements in other languages. Nonetheless the syntax is very powerful and lets us do much more than simply accessing an element in a given position, as we will see bellow.
One important thing to mention is that indexes are 0 based, meaning that the first element of the list has index 0. To get started, we will create a list and then access each of its elements by their indexes.
myList = [1,2,3] print (myList) print (myList) print (myList)
But to retrieve multiple elements of the list in one call, we can put the first and last indexes we want separated by : and all those elements will be returned, as can be seen bellow. Note that the index at the right is excluded from the interval so, for a list with for example 3 elements, we need to put the interval 0:3.
We will also print the type of the returned object to show that in this case it is a list. In the other hand, if we just access one of the indexes, the type of the object will be an integer.
print (myList[0:3]) type(myList) type(myList[0:3])
You can check bellow at figure 3 the expected results of running these commands.
Figure 3 – Accessing the elements of the list by indexes.
Also in this notation we can only specify one of the limits of the interval.
print(myList[0:]) print(myList[:3]) print(myList[1:])
You can check bellow at figure 4 the expected output.
Figure 4 – Output of accessing a list by specifying only one of the limits of the index.
Another interesting feature is that we can use negative indexes to access the elements of the list. So, a negative index considers the elements starting from the end of the list. Thus, the element with index —1 is the last of the list, and so on.
print(myList[-1]) print(myList[-2]) print(myList[-3])
Figure 5 shows the expected result of accessing the list with negative indexes
Figure 5 – Accessing the list with negative indexes.
Naturally we can update an element of the list this way, by accessing the wanted index and assigning a new value.
testList = ['a', 'b', 'c'] print(testList) testList = 1 testList = 2 testList = 3 print(testList)
The output of these operations is shown in figure 6 bellow.
Figure 6 – Updating elements of the list.
Useful methods and functions
A very common operation that we do on lists is appending content to them. To do so, we simply need to call the append method, passing as input the object we want to add to the end of the list.
testList = ['a', 'b', 'c'] testList.append('d') print(testList)
Alternatively, we can use the extend method to append a list to another list.
toAppendList = ['e', 'f'] testList.extend(toAppendList) print(testList)
If instead of appending we want to insert the new element at a given index, then we can use the insert method, passing as first argument the index and as second the object.
myList = [1,3] myList.insert(1,2) print(myList)
To remove an object, we can use the remove method, passing as input the object we want to remove from the list. Note that the remove method doesn’t return the element removed.
myList = [1,2,3] myList.remove(1) print(myList)
You can check bellow at figure 7 the output of all of the previous commands of this section.
Figure 7 – Output of executing the append, insert and remove operations.
Other way of removing an element of the list is by using the del operator. It allows to specify the index of the element we want to remove. Note however that this is not a method of the list and thus, as shown bellow, the syntax is different.
This call also doesn’t return the element deleted. Additionally we can use the pop method also to remove an element from the list at a specific index, but in this case it is returned.
intList = [1,2,3,4] del intList print(intList) popedElement = intList.pop(0) print(popedElement)
Other important operation is getting the length on the list. To do so, we simply need do call the len function and passing as input the list.
testList = [1,2,3,4,5,6,7,8,9,10] print(len(testList))
Figure 8 shows the output of these commands.
Figure 8 – Output of the del, pop and len functionalities.
One operation that we can easily do with lists without calling any method is concatenation. To do it, we simply need to use the + operator.
myList = [1,2,3] concatList = [4,5] finalList = myList + concatList print(finalList)
Other useful operation is repetition, which is performed using the * operator. This can be used, for example, to initialize an array with a specific value.
myList =  print(myList * 10)
We can also check if an element is part of a list using the in operator, avoiding the need for coding a search loop.
myList = ['a','b','c'] print('a' in myList) print('d' in myList)
You can check the expected output of these 3 operators in figure 9.
Figure 9 – Output of the +, * and in operators.
In order to iterate the whole list, we can do the conventional approach of doing a loop between 0 and the length of the list. Nonetheless, we can simply do a for in loop, which will iterate the whole list and put the current element in a variable. This leads for a cleaner and more compact syntax, which is also less error prone.
myList = ['a', 'b', 'c', 'd'] for element in myList: print(element)
The expected output can be seen bellow in figure 10.
Figure 10 – Iteration of a list in a for in loop.
As can be seen by this tutorial, lists are very easy to use and yet very powerful. So, in order to take advantage of the full potencial of MicroPython, it’s important to know its data structures and how to use them.
Note that we only covered some introductory functionalities, since Python lists have much more to offer.