0

$USD

$USD
TUTORIALS ESP32ESP8266MicroPython

ESP32 / ESP8266 MicroPython Tutorial: Working with lists

DFRobot Aug 29 2017 1579

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.

1 intList = [1,2,3,4,5]
2 print(intList)
1  stringList = ['a','b','c','d','e']
2  print(stringList)
2  type(stringList)

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.

1  print (myList[0:3])
2  type(myList[0])
3  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.

1  print(myList[0:])
2  print(myList[:3])
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.

1  print(myList[-1])
2  print(myList[-2])
3  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.

1  testList = ['a', 'b', 'c']
2  print(testList)

4  testList[0] = 1
5  testList[1] = 2
6  testList[2] = 3
7  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.

1  testList = ['a', 'b', 'c']
2  testList.append('d')
3  print(testList)

Alternatively, we can use the extend method to append a list to another list.

1  toAppendList = ['e', 'f']
2  testList.extend(toAppendList)
3  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.

1  myList = [1,3]
2  myList.insert(1,2)
3  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.

1  myList = [1,2,3]
2  myList.remove(1)
3  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.

1  intList = [1,2,3,4]
2  del intList[0]
3  print(intList)

5  popedElement = intList.pop(0)
6  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.

1  testList = [1,2,3,4,5,6,7,8,9,10]
2  print(len(testList))

Figure 8 shows the output of these commands.
Figure 8 – Output of the del, pop and len functionalities.

Other operations

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.

1 myList = [1,2,3]
2  concatList = [4,5]
3
4  finalList = myList + concatList
5  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.

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']

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.

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