6. Working with lists in Python
Oct 10, 2023
Lists are a really useful and versatile variable type. We can store more than a single value in a list. Lists can store more than one type of variable at a time. The order is changeable and we can add or remove items with ease. There are many useful methods to help us work with lists like .append(), .remove(), .pop(), .sort() and many more. When interacting with lists we need to learn how to slice. This means we need to learn python indexing which starts at 0 and can also work in reverse from the back. The slice start index is included in the slide but the stop index is not included.
Copyright image attribution:
https://en.wikipedia.org/wiki/The_Lord_of_the_Rings:_The_Fellowship_of_the_Ring#/media/File:Fellowship.JPG
As a material engineer, one of the most important data types that you are going to be using
is a list. This is important, just think about what you would gather in the lab. When you
collect data, you are plotting it on the X and Y axes, and let's say that you take that
data from your lab, and you now want to try and fit it to some sort of curve, right? And
as you do this fitting operation, we know that we are going to have to be taking into
account each and every one of these data points, and so we would really like a way that allows
us to store X versus Y, where we can compare or access these data points in a systematic
way, and lists allow us to do that. There are other ways, but we will start with lists.
One of the challenges, though, I find is that people, when you first learn about lists,
the way that they get indexed is really confusing. So we said earlier that strings are a list
of characters, right? The string is a word, but that is made up of a bunch of characters
which have to be kept in a certain sequence, right? So here, imagine this word. The word
Iron Man is made up of these 1, 2, 3, 4, 5, 6, 7 letters. When we index them in Python,
we do not start from 1. I didn't make this rule up, so don't blame me. You start from
0, right? So I would be in the 0 position, R1, O2, and so forth, right? You can also
do this in reverse, but what's bonkers is when they go in reverse, they start with 1.
They go negative 1 instead of negative 0 because what's a negative 0, right? So they start
with negative 1, okay? So this is how Python indexes numbers. Now, we've talked before
about slicing. Slicing is important because it allows you to access just certain portions
of a list, right? So here's how it works. If your variable name was variable, right,
you would put these square brackets, and then you have a start index and a stop index, and
you put the colon between those two. How it works, and this is very important, in Python
when you're slicing, you include the start index, you do not include the stop index.
When you're performing a slice, you include the one at this position, you do not include
that one. So from 2 to 5, that would be taking the value starting at 2 and going up but stopping
before you get to 5. So it would be these three blue letters. If you called for that,
it would deliver you O, N, and M. Or if you said from negative 6 to negative 2, again,
it would go to the first position, negative 6, that's R, include it, and it would go up
into negative 2 but it would not include negative 2. So it would give you these four letters.
So that is slicing and Python indexing of lists. What else can we say? So lists are
ordered, meaning the entries go in a specific order, but we can modify it, right? We can
introduce something. So let's do some of this in Python. Let's go ahead and pull up a spider
and do some examples. I'm going to start by typing a list for the Fellowship of the Rings.
We're going to type out the members of the Fellowship. You need to use square brackets
when doing a list and you're going to separate the values with commas. By the way, if you
run out of space, you can continue on to the next line. That's no big deal. Okay, that's
all nine of them. So let's go ahead and start by defining this list. Okay, it knows that
it's a list and it's provided all the names of them in this list. By the way, if you double
click this, it pulls this up and you can see them, right? Now they've been indexed. You
can see the index. It started with zero and it went up to eight, right? So nine in total
if you include the zero, right? There's lots of things that we can do to this list. We
can sort it. There's a built-in method that allows us to sort things. So we're going to
go fellowship and then we're going to type. And if you don't remember, you can tab over
and here's all these different methods and functions that you can work with with this
list. So you could reverse it, for example. Let's sort it first though. So we're going
to do sort, like so. And now you see that our list has now been alphabetically ordered.
If these were numeric values, it would numerically sort them, right? That's one thing that we
can do. Alright, what else can we do with these? You can add something new to the list,
either plugging it in in the middle or adding it on to the end. Adding it on is the easiest.
We use the append, right? So the append command we're going to use for that. So we're going
to do append and if there is a new member, I guess you could say Gollum sort of joins
the party. So we will add him. Now our fellowship list has grown. It's the 9 from before and
we added a Gollum and it put it at the end of the list. Now we, spoiler alert, lose Boromir.
So let's take out Boromir. So let's do that command. We're going to do fellowship.remove
and then we're going to remove Boromir. That's one way to do it. You can also remove it by
telling it to remove one at a certain position, right? So let's try this one first. Now if
we pull up here, our Boromir has been removed. We could also remove it by pulling out a certain
position. So let's comment this out and instead let's try doing this. Boromir was at the previously,
he was at position 8 before we removed him. Another thing you can do, the same thing could
be accomplished by doing the pop command. We could pop position 8. So I'm going to comment
this out. So first thing we'll do is let's repopulate our fellowship as it was in the
beginning. So F9 on that line. That's going to put our line up here. Oh, we have to run this
whole line since it was spread over two lines. It was confused by that. So let's just run this
whole line, that whole section. Let's run that with F9. Okay. So there's our fellowship. It has
Boromir. Now let's try and remove that position. Okay. It removed it, but Gollum was not added
because we didn't run that line. Okay. So everything's working great. What else can we
do? Let's add one. Let's insert something in the middle. Okay. So let's try doing that. Let's try
inserting Gollum at the second position. So we'll comment this out. So instead of appending column,
we're going to do insert Gollum and we're going to insert him at position 2. Remember that means
actually the third one because we start at 0, 1, 2. So it's gonna be the third one. So let's try
that. Okay. So we had our syntax wrong. So the number needs to come first and then the item that
we're going to put into the list. So 2. Let's try that now. Okay. We pull this up and there we go.
Gollum is now there. So lots of things you can do. Lists are not permanent. You can change them.
Okay. What else can we say about these things? Multiple identical entries are allowed and we
already said that you can add other things. Like for example, we could say you could add another
data type to this, right? We could say like fellowship.append and we could add a number.
Even though everything else has been a string so far, doesn't matter. We can still add it. If we
come up here, we'll see that there's now an integer type variable stored in this last item of our
list. No big deal. You can do the length function and it will tell you the number of items in your
list. This is helpful. So we could come down here and we can say, what is the length of fellowship?
And when we run that, let's store that into a variable. Well, let's just actually drop this
into our console, right? If we put that right into our console and run it, it will tell us that there
are 11 items. And sure enough, if we count this thing, we would find that there are 11. It tells
us, by the way, the size is 11 and that's starting from 0 to 10. Therefore, 11 items are in this list.
And then you can concatenate multiple lists together, right? So let's say that you're doing
a grocery list, right? Grocery list one has the following items in it. Apples and bananas. But
then you are getting to the store and you realize that you have another grocery list that your wife
prepared for you. And so you're like, oh shoot, we also need donuts and we also need hot chocolate.
It's possible to combine these, right? So we can say that our new list is going to be equal to
grocery list one. By the way, this auto-completes those, which is really nice. If you hit down there
tab, it will try and guess what your variable is. So we've got that. We can go ahead and run this.
Now we're going to see that our new list, the grocery list, combines all of these things. It
has both the apples, bananas, and the donuts and the hot chocolate. And it included them in the
same order in which I combined them there. If we had switched the order here and done 2 plus 1,
what will it give us? I bet you can already guess. It's just going to reverse those. It's going to
do first donuts and hot chocolate and then apples and bananas, okay? So there's lots of other really
useful methods that you can look into. We've talked about append, insert. There's extend.
This will append elements from another list, right? So if you have two lists, you can extend them.
There's remove, which will remove a certain type. We already talked about that. Pop does the same
thing. You can clear a list by hitting clear, right? So we could do, once you pick up those
things at the grocery store, you could do new list and then you could type clear.
And when you run this, this is just going to be an empty list, right? There's nothing in there,
okay? What else can we do with lists? There's copy. This is really important. Let's do an
example here, right? So let's do this. Let's say that third grocery list is equal to grocery list
one, okay? So we created a new variable that we're calling third list and it's just the same
as what we initially had. Now what happens if we go to our initial grocery list, grocery list one,
and then we're going to remove one of the items, right? You're going to pop the bananas off at the
one at position one. So now, oops, grocery list. We need to spell that correctly. There we go.
Let's try that. So if we run that now, we have removed. So take a look at that. We removed this
list, the bananas from list one, but look what it also did. Our third list was equal to list one,
so it actually removed it from both of those. So remember, this is a key thing about lists.
If you change one and something else was being pointed to that, then you hadn't made a copy of
that other one. If we wanted instead to keep a copy of our original list and call it third list,
or we could say like copy of list one, what we need to do is we need to go to grocery list one
and then use the copy command. That will create a copy. Now, if we run this whole sequence,
check it out. Now copy of list one up here, you can see retained its apples and bananas,
even though grocery list one got rid of the bananas, right? So that's an important thing
to recall with list is that when you remember to make a copy, if you need to preserve the original,
because just renaming it won't create a new one, it'll just point to the same object,
right? And then you can count, you can count how many instances are in an element,
and that's pretty easy. So we won't bother with that. Okay, so that is a primer on using lists
in Python, we will do a lot more of these later, when we learn about how to do for loops, we'll
learn how to go sequentially through a list and maybe take actions on the items that are present.
But this is a primer for now on what lists are and how we use them.

