A Physicist’s Guide to Lists in Python
By: Rhett Allain
Tags: Python, Data Science, Physics, Programming

Photo: Rhett Allain

Physics isn’t just physics. You have to do a lot of other stuff too — like reading, writing, math, drawing, communicating…and yes…programming.

For me, I prefer to use python. OK, I really like Glowscript VPython. It’s basically python but with some pre-loaded modules that has stuff to handle vectors, 3D objects, graphs and more. Yes, it’s my favorite. Oh, and it runs in a web browser too.

Here is a List

I previously created an introduction to functions in python, so now it’s time for lists. In python, lists are sort of like a list you make of your groceries. Let’s just make a simple list. Check this out (code is online).

Here is the output:

There you go. A list. Here are some notes:

You can name a list as you would any python variable. It must be a unique name and it can’t start with a number.

The list is created by using square brackets [].

The items in the list are separated by commas.

The list items can be anything. They don’t have to be just numbers, they can be a mix of stuff.

Here is another list.

This crazy list includes a vector, a string, another list, and a number. It’s awesome.

Addressing List Items

So, you’ve go a list. What now? Let’s make a list of numbers (I’m not really going to use strings in my lists since I am focusing on physics stuff).

things=[2,8,1,3,0]

Here are 5 numbers. Suppose I want to just print out the second element (the 8). That would look like this:

print(things[1])

If you want to print out the first element, that would be things[0] — yes, the items are numbered starting from 0 (not 1).

Here are a couple of other important things about lists.

You can find the length (the number of items in a list) with len(things) — which would be 5 in this case.

— which would be 5 in this case. It’s possible to go through a list backwards. If you use an index of -1 ( things[-1] ), it gives you the last item in the list things[-2] is the second to last item.

), it gives you the last item in the list is the second to last item. If you want to change one item in a list, you can do that. Just do something like things[-1]=20 . This will change the last item from 0 to 20.

. This will change the last item from 0 to 20. You can make a list consisting of lists. This is pretty cool and it’s useful to make things that are sort of like matrices. I’ll cover this in more detail later.

Finally, remember that glowscript.org and trinket.io aren’t real python. They actually take your code and convert it to javascript. This means that if you make two variables equal to the same list, changing one value changes the other. Just be careful.

Adding and Removing Items

Let’s make a list consisting of 10 random numbers. Remember that for GlowScript Vpython, random() returns a random number between 0 and 1.

Here is the plan.

Make a loop that goes up to 10 (but not to 11). You could do this a bunch of different ways, but if you have followed my other python stuff for physics — the while loop is very common.

In the loop, generate a random number.

Add this to the list.

Repeat.

Shouldn’t be too difficult. Here is what it looks like (with some comments afterwards). The code is online.

See. It worked. Now for some comments:

Line 7: I start off with an empty list. There’s nothing in it. But it’s difficult to add to a list that doesn’t exist, so you need to make one first.

Line 4 and 10: I think it’s pretty obvious these are the max number in the list and a “counter” for the list.

Line 12: in the while loop, you need to put <= (less than or equal to) so that it gets all the way up to 10. If you aren’t sure, you can always just make a loop that prints out “n” to check that it’s working correctly (that’s what I do).

(less than or equal to) so that it gets all the way up to 10. If you aren’t sure, you can always just make a loop that prints out “n” to check that it’s working correctly (that’s what I do). Line 14: This adds an element to the list. The thing that you want to add has to be in square brackets.

Pretty cool, right? I like adding to the list with the square brackets because it just makes sense to me. If you don’t think that’s sophisticated enough, you could do this:

rando.append(temp)

Same thing.

What if you want to remove items from a list? Here are some options.

rando.pop(2) — this removes the [2] item (the third) from the list.

— this removes the [2] item (the third) from the list. rando.insert(2,3) — this puts the number 3 at the [2] position.

— this puts the number 3 at the [2] position. rando.clear() — this removes everything from the list (not sure why you want to do that).

— this removes everything from the list (not sure why you want to do that). There’s some other stuff — but I don’t really use it too much. Here is a list of list stuff if you need more.

Traversing a List

You know I’m working my way up to an example using lists — right? Well, one of the things you need to do is to go through each item in a list and do something (physics stuff).

I’m going to show you two ways to go through a list (although there are more). Let me start with a list of 5 vectors — I just feel like using vectors. Here is my list:

Yes, you can have a list continue onto a second line — just end the first line with a comma and you should be fine. Now let’s go through each item in the list and print the magnitude of the vector. Remember that in GlowScript mag() is a built in function that returns the magnitude of a vector. Here’s one way to do this. Comments to follow.

It’s really cool. The for thing in list: does exactly what you would think it would. It temporarily assigns the variable thing to each item in the list. In this case, tempvector is each vector in the list ( vectorstuff ). Then you can just treat that like you would any other vector. Super simple.

When would this method not be the best? What if you need to look at two items in the list at the same time? What if you want to calculate the distance between two position vectors in the list? In this case, you might need to use another method. Check this out.

The output in this method is exactly the same as before. So, here are some notes:

len(vectorstuff) returns the length of the list (but you already knew that). range(len(vectorstuff)) makes a list of numbers that’s the same length as the vectorstuff list. Now the variable n is a number, not the actual list item.

returns the length of the list (but you already knew that). makes a list of numbers that’s the same length as the vectorstuff list. Now the variable n is a number, not the actual list item. With this index (n), vectorstuff[n] is the nth item.

Let’s do some physics.

Example: A Bunch of Projectiles

The cool thing about GlowScript is the 3D aspect. I’m not going into all the details, but here is how you could make a projectile motion problem display in 3D.

But I don’t want to make one ball. I want to make MANY balls. How many? N balls. Here is how this going to work.

I’m going to make a list of balls. Yes, you can put objects (like the sphere() ) into a list.

) into a list. All the balls will start at the same location, but they are going to be launched at different angles. These angles will be evenly distributed between 0 and 90 degrees.

For the projectile motion part, I am going to go through each element in my list: update the velocity and update the position.

Here is the code. I’m going to go over this stuff in two parts. First, this is the set up stuff.

Comments:

Line 6: make an empty balls list.

Line 7: All the balls will have the same starting velocity. This is that velocity.

Line 9,10: This is the starting angle and then the angle step size (pi/2 divided by the number of balls)

Line 13: for the list build, I use the index “i” in the range of the list.

Line 14–15: adds a ball to the list. The make_trail=True just means the balls will leave a trail and look cool.

just means the balls will leave a trail and look cool. Line 18,19: this is the time and the time step (for the numerical calculation).

Now for the code that runs the thing.

Really, this is really similar to the loop for a single ball.

Line 21: the while loop is for 1 second. Different launch angles will take different times, but they should all be finished by 1 second.

Line 22: Remember, rate(100) is a GlowScript thing. This tells the code to not run any more than 100 loops per second. Since the time step is 0.01 second, a rate of 100 should run in real time.

is a GlowScript thing. This tells the code to not run any more than 100 loops per second. Since the time step is 0.01 second, a rate of 100 should run in real time. Line 24: Here is a loop in the main time loop. This loop just goes through each item in the balls list.

Line 25, 26: since I am calling each item “ball”, I can just do stuff that looks like a normal line for a single ball. That’s nice.

Line 27: This is just a check to make sure the ball “hits” the ground. If so, it stops.

So, what does it look like when you run it? Like this:

But wait. What if you want MORE BALLS? You just need to change N to 50 and you get this.

It’s not just physics, it’s art. If you want to spice it up, you could try changing the ball colors and stuff like that.

Example: Using a List for Data

There is one more very useful way to use a list — for data. Yes, let’s say that you get some position-time data in a spreadsheet or something. Maybe this came from a video analysis or data from a sensor. Who knows.

Yes, it’s very possible that you could plot this data in the spreadsheet or some other very awesome graphing program. But what if you want to plot this along with a numerical calculation. Or maybe you need to do some calculations to the data. Whatever the reason, I’m going to show you how to plot this in python using a list.

Let’s start with some data. Here is position-time data from David Blaine’s ascension stunt (with balloons) — full analysis here.

So, here’s what I do. I’m going to start with the time column. I’ll highlight all the numbers and copy them. Then paste them into a list in GlowScript. I’ll do the same for the position data.

When I paste it into GlowScript, it’s just a bunch of numbers one on each line. I usually just manually add the comas and put them in one line. If you have a giant list of numbers, it might make more sense to copy them first into a word processor so you can “find and replace” the return-lines with comas.

Yes, I also know that in real python you can open a file. Actually, you can do this in GlowScript too — but the file has to be on some server somewhere. I just find it’s easier to copy and paste the numbers into GlowScript.

Now, to plot them I just need to traverse the lists and use normal graphing stuff (here is my graphing tutorial). Note: if your two data lists have a different number of elements, you are going to have a bad time.

Here is the code to plot these two.

I really don’t have any extra comments on this. Here is the graph that it creates.

Cool. OK, I think that’s a good start to lists in python.