A list in Python is just that: a list of things. We use lists all the time: shopping lists, TO-DO lists. Even Santa uses lists.

Lists in Python have features that jive with our general idea of lists:

  • They can be empty.
  • They can have any number of things in them.
  • They can have different kinds of things in them.
  • You can append (add to the end) new things to them.
  • You can insert new things anywhere in them (this is easier with lists that aren't written on paper)
  • You can sort them to put them in some particular order (if everything in them can be compared).
  • You can see how long they are.
  • You can remove things from them.
  • You can replace things in them.
  • You can check if something is in them.
  • You can combine them.
  • You can throw them out when you're done with them.

Making a list looks a lot like making a tuple, except that square brackets are used:

Download: file
>>> my_list = [1, 2, "three"]

In Python, lists and tuples are both a kind of sequence, which means that they have many capabilities in common. For example, you can find their length:

Download: file
>>> len(my_list)
3

Accessing items is the same:

Download: file
>>> my_list[0]
1
>>> my_list[1]
2

If you use negative indices, they are from the end rather than the start. That makes sense, but -1 is the last item, -2 is the second to last, etc. There really isn't a way around this since -0 isn't really a thing. You can use negative indices with tuples as well as with lists, but it isn't generally as useful. Tuples tend to be small, and for a specific purpose they tend to be the same size with the same type of information in each position. In our tuple example, the frequency was always at position 0, and the duration at position 1.

Lists are more dynamic, both in size and content, unlike tuples.

You can change the contents/size of lists but not tuples! In exchange, tuples use less memory and are great when you want 'immutable' data

Changing the thing at a specific location of a list is much like accessing whatever is there: you simply give it a new value.

Download: file
>>> my_list[2] = 42
>>> my_list
[1, 2, 42]

Appending new items to a list is easy:

Download: file
>>> my_list.append(3)
>>> my_list
[1, 2, 42, 3]

As is inserting something anywhere in the list:

Download: file
>>> my_list.insert(1, 99)
>>> my_list
[1, 99, 2, 42, 3]

The first argument to insert is where in the list to put the new item, at index 1 in the above example (i.e. the second position). Everything else will get moved to one position larger to make room.

Lists have many more capabilities that we won't consider here. See the python documentation for more information.

Making songs

We used tuples to form notes that can be played by combining a frequency and a duration, and we wrote a function to pull that information out of a tuple and play the tone on the CircuitPlayground Express' built-in speaker. The next step is to put those individual notes together into songs. Before we can do that we need to make a slight adjustment to the play_note function to add rests. We can make the decision to use a frequency of 0 Hertz indicate a rest, or silence:

Download: file
def play_note(note):
    if note[0] != 0:
        pwm = pulseio.PWMOut(board.D12, duty_cycle = 0, frequency=note[0])
        pwm.duty_cycle = 0x7FFF
    time.sleep(note[1])
    if note[0] != 0:
        pwm.deinit()

With that we can now construct a list of note tuples.

import time
import board
import pulseio

C4     = 261
C_SH_4 = 277
D4     = 293
D_SH_4 = 311
E4     = 329
F4     = 349
F_SH_4 = 369
G4     = 392
G_SH_4 = 415
A4     = 440
A_SH_4 = 466
B4     = 493

twinkle = [(C4, 0.5), (C4, 0.5), (G4, 0.5), (G4, 0.5), (A4, 0.5), (A4, 0.5), (G4, 0.5), (0, 0.5),
           (F4, 0.5), (F4, 0.5), (E4, 0.5), (E4, 0.5), (D4, 0.5), (D4, 0.5), (C4, 0.5)]

def play_note(note):
    if note[0] != 0:
        pwm = pulseio.PWMOut(board.D12, duty_cycle = 0, frequency=note[0])
        # Hex 7FFF (binary 0111111111111111) is half of the largest value for a 16-bit int,
        # i.e. 50%
        pwm.duty_cycle = 0x7FFF
    time.sleep(note[1])
    if note[0] != 0:
        pwm.deinit()

def play_song(song):
    for note in song:
        play_note(note)

play_song(twinkle)
This guide was first published on Jul 13, 2018. It was last updated on Jul 13, 2018. This page (List) was last updated on Aug 22, 2019.