VPython Lists
Yu Zhou Fall 2025
An introduction to creating and using lists in VPython.
The Main Idea
Let's say you want to create 100 or maybe even 12,000 tennis balls to help you stop a 17, 500 kg block of mass sliding right at you using VPython. Now, you could perhaps do it like this:
ball1 = sphere(color=color.blue, radius=0.2) ball2 = sphere(color=color.blue, radius=0.2) ball3 = sphere(color=color.blue, radius=0.2) ball4 = sphere(color=color.blue, radius=0.2) ball5 = sphere(color=color.blue, radius=0.2)
and so on and so on. However, this can get really disorganized, and you probably don't want to count all the way to 'ball12000'. Fortunately, we can use lists to contain all of these balls!
# This is where we create our list
ball_list = []
# We set how many balls to add to our list
num_balls = 1000
# Inside this loop, we add num_balls amount of balls to the list
for i in range(num_balls):
# Every time we add a ball, we put it next to the previous ball
pos = vector(0, 0, i)
# radius is 1 for the ball
radius = 1
# We create the ball
ball = sphere(pos=pos, radius=radius, color=color.red)
# Finally, we add it to the list
ball_list.append(ball)
Now that's a lot of balls.
VPython lists are pretty flexible unlike lists from other languages such as:
- containing duplicate items
- easily modifying, replacing, or removing elements
- keeps track of the order elements were added in
- elements accessed through their position
- containing different data types (integers, strings, booleans, etc)
A Computational Model
In creating the basic list, there are two components: the name and the contents. If we look at the first part of this line of code, we can see that we have named the list as 'ball_list'.
ball_list = [];
A list can have any kind of name. It could be called 'x', 'alotofballs','billnyethescienceguy', or anything you want. However, the best names are intuitive and help both you and other people understand your code easily.
The second part of creating a list is the contents. The contents of a list can either be empty or have something already there.
ball1 = sphere(color=color.blue, radius=0.1) ball2 = sphere(color=color.red, radius=0.2) ball3 = sphere(color=color.yellow, radius=0.3) ball_list = [ball1, ball2, ball3] box_list = [] duck_list = [sphere(color=color.white, radius=0.2)]
These are just some examples!
Lists are dynamic arrays that automatically resizes when needed. When an object is stored in a list, it is stored as a reference so changing an item in the list will update itself in the scene instantly. The access cost is also O(1), making updates and finding objects fast. For searching, the time complexity is O(n). It takes a while, but it gets the job done.
Examples
Simple
Let's say we want to create 5 disco balls and make them change colors repeatedly. We first create a list and set how many balls we want. Afterwards, we add all of the balls into the scene and the list. Finally, we create a loop that changes the color of each ball depending if the variable 't' is even or odd.
ball_list = []
num_balls = 5
for i in range(num_balls):
pos = vector(i*2-3, 0, 0)
radius = 1
ball = sphere(pos=pos, radius=radius, color=color.white)
ball_list.append(ball)
t = 0
dt = 1
while True:
rate(100)
t += dt
if t % 2 == 0 :
ball_list[0].color = color.blue
ball_list[1].color = color.red
ball_list[2].color = color.green
ball_list[3].color = color.yellow
ball_list[4].color = color.orange
else :
ball_list[0].color = color.red
ball_list[1].color = color.green
ball_list[2].color = color.blue
ball_list[3].color = color.purple
ball_list[4].color = color.yellow
Difficult
For more of a challenge, let's create a circle of balls and make them rotate! First we would need to create a few variables just to define how many balls and how big the circle is. For this example, we use two lists. One for the individual balls, and the other for their corresponding position/angle in the circle. We can see now how important it is that we can access each ball and its position quickly using a list. Finally, we create a while loop to continuosly update the balls' position so that they appear to be rotating in a circle!
For a bonus challenge, what would we do to make the balls turn in the opposite direction?
num_balls = 50
radius = 5.0
omega = 0.5
balls = []
angles = []
for k in range(num_balls):
theta = 2*3.14 * k / num_balls
pos = vector(radius * cos(theta), radius * sin(theta), 0)
b = sphere(pos=pos, radius=0.15, color=color.cyan)
balls.append(b)
angles.append(theta)
dt = 0.01
while True:
rate(100)
for k in range(num_balls):
angles[k] += omega * dt
theta = angles[k]
balls[k].pos = vector(radius * cos(theta), radius * sin(theta), 0)