[MUSIC] Unlike the other Python data types that we've seen so far, lists are mutable, meaning you can change them. So the contents of a list are not fixed. That means during the execution of the program you can actually change what's stored within a particular list. So let's take a look at some of the different ways in which you can do that. One of the ways that you can change the contents of a list is to reassign one of the values that is stored at a particular index within the list. And to do so we'll use a syntax that hopefully is already familiar to you. Okay, we use the open close bracket indexing syntax. So you can see here I created a new list using the range function. So lst should be a list that has the values 0, 1, 2, 3, 4 contained in it. I want to reassign what is stored at index 1 and index 3. And so you can see here I have lst[1]. Like I said, you should recognize that as the syntax we used to access the element at index 1 within the list. But now I have it on the left-hand side of an equal sign, so if I follow it up by an equal sign it seems I want to reassign the value that is stored at that location, right? So I'm going to reassign it to be -7. Similarly, I'm going to change the value that is stored at index 3 within the list to be 17. And, let's print out this list. Let's see the before and after. Okay, as you can see originally it was [0, 1, 2, 3, 4]. Now, at locations 1 and 3, we have -7 and 17 instead of 1 and 3. So we have actually now mutated this list. Now this is not the only way that we can change the contents of a list. Lists also have methods that allow us to change what's in the list and in particular, we can add things to the list, right? So let's take a look at the append and insert methods, okay. So append basically adds another element onto the end of the list. So the size of the list actually now grows, okay? What insert does, this says well, I don't want to add it at the end, I want to add it at a particular index, okay? So lst.insert(2, 75) will add the element 75 at index 2 and let's see what that means exactly. All right, so you can see here 42 was added to the end of the list by calling append. And insert now put 75 at index 2 and when it did that it didn't replace what was at index 2. It pushed everything to the right so to speak. Lists have another method that allows me to add items to the list called extend. And extend is a little bit different than append in that you can add multiple items at once. Okay, so instead of just taking a single item, extend takes another sequence, such as a list, and it adds all of the elements of that list, one by one, to the end of the original list. So here I have lst2 which has three items. I call lst.extend(lst2) and it's going to put those three items into lst. And so you can see here, -56, 27 and 8 were all added one by one to the end of lst. Now, people sometimes mix things up here and accidentally use append when they meant extend. And so let's see what happens here. Okay so I'm going to append lst2 after I extended with lst2 and you can see what happened the actual entire list got appended to the end of lst. Right, and so you can see the square braces here, indicating that the last item of lst is now a list. Now if you remember when we talked about lists originally, I said it's not a good idea to mix the types that you have within a list. So here I have a bunch of integers and then the last element is a list. This is probably not what you meant. You probably meant to use extend, accidentally used append. If you see this, that's what happened. Now, it wouldn't be any fun if we could only add items to lists, right? We want to be able to take them away as well. So, let's remove some items from the list. Let's have a method called pop that basically gets rid of or removes the last item in the list. It actually returns it but I'm ignoring that here. Let's run this, so I screwed up with append, where I appended that list when I didn't mean to. Let's pop it back off. And there it goes, right? It's gone. Okay. Now pop by default, if you give it no arguments, will pop the last thing on the list. But you can also give it an index. And if you do that, you can see it removes something at that index. So call lst.pop(3), it takes away whatever item was at index 3, removes it, and then slides the rest of the elements forward or left, which ever way you want to think about it. So there's not an empty space within my list. So pop with no arguments is kind of the inverse of append and pop with a Index argument is kind of the inverse of insert. Now we've seen how you can actually change or mutate the contents of a list. In particular, in this video, we saw how to change the element that's stored at a particular index in the list. We saw how to add items to lists and remove items from a list. So in most programs, you're going to use these operations. You're not just going to create all your lists and leave them as they are for the remainder of the program. Perhaps you're creating elements as the program executes and you want to add them to the list or maybe you're processing the elements of a list and after you've processed them, you want to remove them. So now we know how to do that.