MicroPython ‘list’ Data Type

Contents

Introduction

MicroPython is a “slim” version of Python specifically designed with a small footprint to efficiently run on memory constrained microcontrollers.

Computer languages have a built-in set of data types along with user capability to define custom types. MicroPython is no exception. Following are the standard or built-in data type for MicroPython:

  • Numeric
  • Boolean
  • Sequence Type
  • Set
  • Dictionary
Python data type hierarchy
Source: geeksforgeeks.org

Lists are part of the MicroPython sequence family which also include tuples and strings. Considered another way, lists can also be considered a collection type

There are four collection data types in the MicroPython programming language:

  • list is a collection which is ordered and changeable.
  • tuple is a collection which is ordered and unchangeable.
  • set is a collection which is unordered, unchangeable[1] and unindexed.
  • dictionary is a collection which is ordered[2] and changeable.

Lists

Lists are positionally ordered collections of arbitrary objects and they have no fixed sizes. They are similar to arrays in other languages but much more powerful. Features include:

  • No fixed size i.e. they can grow or shrink in size.
  • No fixed data type i.e. can have mixed types.

Lists are mutable. They can be modified in place by:

  • Assignment to offsets e.g. L1[2] = 54.319
  • Variety of list method calls

Lists may contain duplicate elements.

The elements of a list are enclosed in square brackets [ ]. A single element of a list is assessable using simple indexing; list[index]. Multiple elements are obtainable with the slice operator list[start : end]:.

It is important to note:

  • Indexing begins at 0
  • The end index numbered element of a slice operation is not returned. For example MyList[0 : 2] returns a list with elements MyList[0] and MyList[1].

Adding elements to a list
list1 = [123, 'spam', ['brown', 'red', 'blue'], 5.89]
list2 = []  ⇒ empty list

Simple indexing
list1[1] ⇒ 'spam'
list1[-1] ⇒ 5.89

Slice operator
list1[0 : 2] ⇒ [123, 'spam']
list1[:3] ⇒ [123, 'spam', ['brown', 'red', 'blue']]

          

Note the flexibility of this data type with list1 above having another list as one of its elements.

Other iterable[1] data types (e.g. string, tuple, and dictionary) are convertible to lists using the list() function.


string1 = 'ABC'
list(string1) ⇒ ['A', 'B', 'C']

          

More List Operators

Table 1: MicroPython list Operators
Operator Description
+

Concatenation; joins two or more lists.

['A'] + ['B'] + ['C']
⇒ ['A', 'B', 'C']

*

Repeats the list a given number of times.

['AB', 'CD'] * 2
⇒ ['AB', 'CD', 'AB', 'CD']

==

Comparison operator tests whether two lists are equal

L1 = ['A', 'B', 'C']
L2 = ['A', 'B', 'C']
L3 = ['A', 'C', 'B']
L1 == L2 ⇒ True
L1 == L3 ⇒ is False

!=

Comparison operator tests whether two values are not equal

L1 = ['A', 'B', 'C']
L2 = ['A', 'C', 'B']
L1 != L2 ⇒ True

in

Membership operator tests if a value is in the list

L1 = ['cat', 'dog']
'dog' in L1 = True

not in

Membership operator tests if a value is not in the list

L1 = ['cat', 'dog']
'rabbit' not in L1 ⇒ True

List Functions

Table 2: MicroPython list Functions
Function Description
len(list)

Returns the number of elements in a list

len(['ABC', 56, [1, 2, 3]])
⇒ 3

max(list)

Returns the maximum value in a list of numbers.

max([100, -5, 76.23]) ⇒ 100
max([100, '-5', 76.23]) ⇒ returns an error

min(list)

Returns the minimum value in a list of numbers.

min([100, -5, 76.23]) ⇒ -5

sorted(item
   [, reverse =
         True])

Returns a copy of the list as a sorted list

L1 = ['d', 'a', 'g']

print(sorted(L1))
⇒ ['a', 'd', 'g']

print(sorted(L1, reverse = True))
⇒ ['g', 'd', 'a']

List Methods

A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.” [codecademy.com]

This series on MicroPython discusses classes and methods here.

Methods are invoked using dot notation i.e. List.Method() with the following table providing simple examples. The table below provides an exhaustive list of the MicroPython list methods.

Table 3: MicroPython list Methods
Method Description
append(item)

Adds an item to the end of the list

list1 = [1, 2, 3]
list1.append(4) ⇒ [1, 2, 3, 4]

extend(iterable)

Add the elements of an iterable[1] to end of the list

list1 = [1, 2, 3]
list1.extend('ABC')
⇒ [1, 2, 3, 'A', 'B', 'C']

insert(pos, item)

Adds an item at the specified position. The first position has an index of 0.

list1 = [1, 2, 3]
list1.insert(1, 'ABC')
⇒ [1, 'ABC', 2, 3]

remove(item)

Removes the first occurrence of item from the list.

list1 = ['A', 'B', 'C', 'B']
list1.remove('B')
⇒ ['A', 'C', 'B']

pop(pos)

Removes the element at the specified position. The first position has an index of 0.

list1 = ['A', 'B', 'C', 'B']
list1.pop(2)
⇒ ['A', 'B', 'B']

clear()

Removes all elements from list.

list1 = ['A', 'B', 'C', 'B']
list1.clear()
⇒ []

sort(
[reverse=True])

Sorts the list.

list1 = ['B', 'D', 'C', 'B']

list1.sort()
⇒ ['B', 'B', 'C', 'D']

list1.sort(reverse = True)
⇒ ['D', 'C', 'B', 'B']

reverse()

Reverses the order of the list.

list1 = ['A', 'B', 'C', 'D']
list1.reverse()
⇒ ['D', 'C', 'B', 'A']

index(item)

Returns the index of the first element of the list with the item value.
Returns an error if item is not an element of the list.

list1 = ['A', 'B', 'C', 'B']
list1.index('B') ⇒ 1
list1.index('Z') ⇒ Error: 'object not in sequence'

count(item)

Returns number of elements in the list with value of item.

list1 = ['A', 'B', 'C', 'B']
list1.count('B') ⇒ 2
list1.count('Z') ⇒ 0

copy()

Returns a copy of the list.

list1 = ['A', 'B', 'C', 'B']
list2 = list1.copy()
list2 ⇒ ['A', 'B', 'C', 'B']