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

Add and Remove List Elements

00:00 In this lesson, you’ll learn how you can add and remove elements from immutable lists. You’ve seen that at work already in the previous lesson when I did it using slice notation.

00:10 But lists actually have useful methods that allow you to add and remove elements. For example, .insert(), where you first pass the index where you want to insert the element and then the element that you want to insert; .append(), in which you just pass an element as an argument and it adds it to the end of the list; .extend(), where you can pass as an argument an iterable, so some sort of a collection, and then it adds it to the end of the list; and .pop(), where you can optionally pass an index, and then it’ll remove the element at that specified index.

00:42 If you don’t pass an index, it’s going to take off the last element and return it.

00:48 Let’s look at those in a little more detail. You can use .insert() to insert an element at an index position. This gives you a lot of flexibility.

00:55 For example, here we’ve got the numbers list consisting of the two elements 1 and 3. And then if you call numbers.insert(), open up parentheses, and then first pass the index position.

01:07 So I want to insert an element at the index one, which is currently where the value 3 sits. And there I want to insert "two", and I’m just using a string here just to make it a little more obvious what happens, because if you have two integers here as arguments, then it might get a little confusing what is the index and what is the value.

01:28 And so you can see that now what Python does is it puts "two" at the position where 3 was before and adds a new slot and moves 3 at index position two that didn’t exist before.

01:41 So that’s pretty powerful.

01:44 You can also use .append() to add an element to the end of the list, which is a very common operation, for example, if you’re looping over some data structure and you want to append specific elements that you found in there to a list.

01:56 In this example, you have again the numbers list consisting of two elements, 1 and 2, and then you say numbers.append(), and then pass the integer 3 in this case as the argument, and then the numbers list is going to consist of three elements, 1, 2, and 3.

02:16 Then you can use .extend() to add a collection at the end of the list. So that’s not just a single element, but more than one element. Same example, numbers list as before, consisting of two elements, 1 and 2, and then you’re calling .extend() on the numbers list and passing it another list.

02:33 So it’s important that this is an iterable that you’re passing to .extend(). This can’t be just a single value. It can be a single value, but it has to be a collection that then contains just one value.

02:44 In this case, it contains two. So you have a list that has two entries in it, 3 and 4, and you pass that as an argument to .extend().

02:52 And then the numbers list looks like this. It starts with the 1 and 2 from the original numbers list.

02:58 And then 3 and 4 is the extended version. And you can see that it’s still the numbers list. So Python mutated this list, added two values at the end, and you can do this with any sort of collection.

03:10 So in the example below, you can see how I’m using .extend() with a tuple. And the tuple contains the values 5 and 6.

03:19 And then these also get added to the numbers list. So then you have a numbers list that starts with element one and goes all the way up to the number six.

03:28 And the final one that we’re going to look at is .pop(), which allows you to remove an element from the list. And you can do that by passing an index position.

03:36 So assume you have this numbers list that goes from one, two, three to four, and then you can say numbers.pop() and pass in the index position 2 as an argument to the .pop() method.

03:48 Then it’s going to remove the element at the index two, which is here, the integer 3.

03:53 And what .pop() does is it actually returns the value that you’re removing from the list. So here you get as an output the integer 3 because that’s what was sitting at the index position two,

04:06 and your numbers list was mutated again. So now what Python did to it, it removed the element at position two, which is the integer 3. And now your list looks like this.

04:15 It contains three elements, the integer 1, the integer 2, and the integer 4.

04:21 If you don’t pass an index position as an argument to .pop(), then it defaults to just removing the last element. The rest works the same as before.

04:31 Okay, a lot of information. Take a moment, breathe,

04:39 and now let’s IDLE.

Become a Member to join the conversation.