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
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
Operator | Description |
---|---|
+ | Concatenation; joins two or more lists. ['A'] + ['B'] + ['C']
|
* | Repeats the list a given number of times. ['AB', 'CD'] * 2
|
== | Comparison operator tests whether two lists are equal L1 = ['A', 'B', 'C']
|
!= | Comparison operator tests whether two values are not equal L1 = ['A', 'B', 'C']
|
in | Membership operator tests if a value is in the list L1 = ['cat', 'dog']
|
not in | Membership operator tests if a value is not in the list L1 = ['cat', 'dog']
|
List Functions
Function | Description |
---|---|
len(list) | Returns the number of elements in a list len(['ABC', 56, [1, 2, 3]])
|
max(list) | Returns the maximum value in a list of numbers. max([100, -5, 76.23]) ⇒ 100
|
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))
print(sorted(L1, reverse = True))
|
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.
Method | Description |
---|---|
append(item) | Adds an item to the end of the list list1 = [1, 2, 3]
|
extend(iterable) | Add the elements of an iterable[1] to end of the list list1 = [1, 2, 3]
|
insert(pos, item) | Adds an item at the specified position. The first position has an index of 0. list1 = [1, 2, 3]
|
remove(item) | Removes the first occurrence of item from the list. list1 = ['A', 'B', 'C', 'B']
|
pop(pos) | Removes the element at the specified position. The first position has an index of 0. list1 = ['A', 'B', 'C', 'B']
|
clear() | Removes all elements from list. list1 = ['A', 'B', 'C', 'B']
|
sort( [reverse=True]) |
Sorts the list. list1 = ['B', 'D', 'C', 'B'] |
reverse() | Reverses the order of the list. list1 = ['A', 'B', 'C', 'D']
|
index(item) | Returns the index of the first element of the list with the item value.
list1 = ['A', 'B', 'C', 'B']
|
count(item) | Returns number of elements in the list with value of item. list1 = ['A', 'B', 'C', 'B']
|
copy() | Returns a copy of the list. list1 = ['A', 'B', 'C', 'B']
|