Arrays and lists in Python Processing

Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Lesson 13 of 20 · Python and Processing

In the earlier lessons we used single variables to keep data. However, in games and animations we often have many objects – several balls, enemies, projectiles or colours.

That is why we use lists (arrays). A list lets us keep several values in one variable.


What are lists?

A list is a collection of elements that can be:

  • numbers
  • text (strings)
  • objects
  • other lists

An example of defining a list:


scores = [0, 0, 0]
colors = ["red", "green", "blue"]
A list can have any number of elements and can change while the program runs.

Accessing the elements of a list

We access elements with an index. In Python the index starts from 0.


print(scores[0])   # the first element
print(colors[2])   # the third element

If a list has 3 elements, the indexes are:

  • 0
  • 1
  • 2

Adding and removing elements

Lists are dynamic – we can add and remove elements.


scores.append(5)           # adds a new element at the end
last_score = scores.pop()  # removes the last element

The most common operations on lists

  • append() – adds an element
  • pop() – removes the last element
  • len() – returns the number of elements

Going through a list (the for loop)

The most common way of working with lists is using a for loop.


for color in colors:
    print(color)

Result:

  • red
  • green
  • blue

Lists in Processing animations

In Processing projects lists are very useful when we want to keep track of several objects at the same time.

For example, we can make a list of balls.


balls = []

for i in range(5):
    balls.append(Ball())

Now we can animate all the balls in the draw() function:


for ball in balls:
    ball.move()
    ball.show()
Now every ball has its own position and behaviour, but we control them all with one list.

A small example: several circles on the screen

The following example uses a list of coordinates to draw several circles:


circles = [100, 200, 300, 400]

def setup():
    size(600,400)

def draw():
    background(240)

    for x in circles:
        ellipse(x,200,40,40)

This program draws several circles using only one list.


Connecting with earlier lessons


Mini project: several circles with a list

Let us make a small interactive example where a list keeps the positions of circles. Every mouse click adds a new circle to the screen.


circles = []

def setup():
    size(600,400)

def draw():
    background(240)

    for c in circles:
        ellipse(c[0], c[1], 40, 40)

def mousePressed():
    circles.append([mouseX, mouseY])

How does this program work?

  • the list circles keeps the positions of the circles
  • every element of the list has the form [x, y]
  • a mouse click adds a new element to the list
  • the draw() loop draws all the circles

In this way we can make an unlimited number of objects in the program.

Practice tasks

  • Create a list of colours and draw squares in those colours
  • Create several balls using a list of objects
  • Add a new ball with a mouse click
  • Make a list of positions and draw a circle at each position

What have we learned?

  • what lists are in Python
  • how to access the elements of a list
  • how to add and remove elements
  • how to use lists in Processing animations

Frequently asked questions

What are lists in Python?

A list is a data structure that lets you keep several values in one variable. The elements of a list can be numbers, text or objects.

What is the difference between a variable and a list?

A variable keeps one value, while a list can contain several values that are accessed with an index.

Why are lists important in games and animations?

Lists make it possible to manage a large number of objects such as enemies, projectiles or balls in an animation.

Can lists contain objects?

Yes. In Python a list can contain any type of data, including objects defined by classes.

The same in Processing with Java

The logic is the same; only the syntax of the language is different.

Check your knowledge

Open the quiz

Mini quiz: lists

1. How do you add an element to the end of the list balls?




2. Why are lists useful in animations?