Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Change Elements in a List

00:00 You’ve made it through the beginning of learning about lists, how to create them, and how to work with lists in the same way that you’ve worked with tuples so far.

00:07 So you may be thinking, why do I even want to work with lists? Well, the thing is you can change elements in the list, and that’s pretty exciting. If you look at this code example, you can see that we’re using the numbers list again.

00:19 And instead of throwing an error, when you try to assign a new value to an index position in the list, it just does it. So the resulting list after this short piece of code where you start with the list [1, 2, 3], and then change the element at index one to 20 is that you have a list that contains the integer 1, the integer 20, and the integer 3.

00:43 So if this is you right now, you’re really excited about this, then you’re right because being able to change elements in the list gives you a lot of flexibility.

00:52 Let’s go ahead and try to play with this a bit over in IDLE. So let me re-create our numbers list,

01:01 [1, 2, 3], and then work with this some more. So as you’ve just seen, you can go ahead and say numbers, open up the square brackets and put in an index number.

01:11 Let’s say index one. So this is pointing to the integer 2. And now instead of just returning it, I’m going to reassign it to a different value.

01:21 So I’m going to say instead of 2, I want to have 20 in that numbers list. And Python does not complain, but instead it actually mutated the list.

01:32 So this is what it means that lists are mutable. You have these slots for values that have a certain index position, and you can go in and change any of the values there.

01:42 So you can just take whatever value is in that slot, swap it out for something else.

01:47 Is that exciting? Well, it gets even better. The size of your list isn’t fixed, so it can grow and shrink as well. After creation, you can go in there and for example, use slice notation to select, let’s say we’re going to select starting from index one and going all the way to the end.

02:05 So I’m going to say [1:] and then omit the end. And I’m going to replace these two values, 2 and 3, with something else.

02:15 I’m going to replace it with the values 200, 300, and 400.

02:24 So when you now inspect numbers, you can see that your list is larger than before. Now it contains four elements, while we started off with only three elements.

02:35 So I’ve just effectively grown the list by one element and replaced a couple of them in there. So the only integer that’s still from the original list is our one at the beginning.

02:47 Okay, let’s shrink the list back and change that to maybe put it back into its original shape. I can say numbers, and I’m going to use this slice notation again and say starting from element

02:59 one up to the end. So this slice is going to give me 200, 300 and 400 as a list.

03:07 So if I reassign this to a shorter list, I can, for example, re-create the previous list. I’m going to replace the slice of these three elements with just two elements, 2 and 3.

03:25 And just as if nothing happened, we mutated the list numbers back to its original shape. You can see like you went through quite a few steps, you addressed one specific element in the list and changed its value.

03:40 You grew the list by assigning more elements to a slice of fewer elements. So in this case, it was you got a slice of two elements, 2 and 3, and you replaced it with a list of three elements, which effectively grew the list by one element.

03:56 And then you did the inverse basically by selecting a slice from the list that is three elements and replaced it with a list that only contains two elements.

04:06 All of this works without any hiccups and without any complaints from Python.

04:11 And the cool thing about the slicing syntax is that you can replace a slice of any size from any point of the list with lists of different sizes.

04:20 This is why lists are pretty powerful because of their flexibility. You can basically create a structure, a collection of elements, that you can throughout the course of your program change as much as you want to.

04:35 And this is what it means that lists are mutable, and that’s something that you can’t do with tuples. So if you need that flexibility, then you should be working with lists.

04:44 If you are worried about this flexibility and you just need a fixed data structure that is immutable and that doesn’t change, then you should be working with a tuple.

04:54 All right, in the next lesson I’ll show you how you can add and remove elements not using slice notation like I did here, but instead using list methods.

Become a Member to join the conversation.