ESP32 / ESP8266 MicroPython Tutorial: Working with lists
DFRobot
Aug 29 20171579
The objective of this micropython 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 ESP32 device integrated in a ESP32 development board.
Introduction
The objective of this micropython 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 lists
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.
Naturally, we can have lists of other types of objects. Bellow we are creating a list of strings.
In both cases, if we check the type of these variables with the type function, it should return list.
1 type(intList)
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. 1 objectList = [1, 2, 'a', 'b', 'c', [ 1, 2, 3] ] 2 print (objectList) 3 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.
1 emptyList = [] 2 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. 2 print (myList[0]) 3 print (myList[1]) 4 print (myList[2])
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.
1 myList = [1,2,3]
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.
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.
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.
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.
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.
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.
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.
1 myList = [0] 2 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.
1 myList = ['a','b','c'] 2 print('a' in myList) 3 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.
Iteration
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.
1 myList = ['a', 'b', 'c', 'd'] 2 3 for element in myList: 4 print(element)
The expected output can be seen bellow in figure 10.
Figure 10 – Iteration of a list in a for in loop.
Final notes
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.
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.
This article takes a closer look at each of these devices, comparing them across key areas like Python support, ease of use, Python programming tools, pin design, and hardware projects.
Deciding between Scratch and Python for your child's coding journey? This article compares both languages based on learning background, goals, methods, and difficulty.