List append vs. List extend in Python

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

In this lesson, you will learn the difference between appending an item to a list and extending a list in Python using the .append() and .extend() methods

Instructor: [00:01] To see the difference between .append() and .extend(), let's start with two lists. On the left, I'm going to start off with a list that's x equal to 1, 2, and 3. In the right-hand pane, we'll say y is equal to a list with the values 1, 2, and 3.

[00:19] On our list x, we're going to append the value 4 to our list. On the right, we're going to use the .extend() method. If we print both of those out, the values look to be the same.

[00:36] Let's do the same thing, but instead of appending just an integer, we're going to append another list. We'll do x.append. Then we'll append the list with the values 5 and 6. Over here on y, we'll do y.extend with the values 5 and 6.

[00:55] If we take a look at x, and then we take a look at y, we see that x has a list of five elements in it. It has the integers 1, 2, 3, and 4. Its fifth element is another list containing the values 5 and 6. When we use the .extend() method, we see that we have a list of six elements, the integers 1 through 6.

[01:17] The difference between .append() and .extend() is that .append() will add the supplied value as a new element in the list regardless of what the value is. .extend(), on the other hand, adds the elements of the supplied list to the original list.

[01:31] As a matter of fact, .append() will work the same for different data types. We can say x.append and append the value of foo. You can see it added the string foo to our list as an element. If we do y.extend with the string foo and then take a look at y, because strings are iterable in Python, .extend() took each element or each character from the string foo and added it to our original list.

[02:00] Let's do one more thing before we wrap this lesson up. I'm going to create another new list. We're going to call this one, conveniently enough, z, give it the values of 1, 2, and 3. I'm going to do real quick here to show you the value of the id(z). Now lets say z += and supply the list 4 and 5.

[02:21] If we take a look at z again, we see that using += was the syntax equivalent of .extend(). If we check our value for z, we see the value remains the same. It's the same list because it was a mutable object, so it just added to it.

[02:40] If we do this, though, if we say z = z +, and then we'll do 6 and 7, we'll take a look at z, and it looks like it extended it, but if I take a look at the id of z, we have a new id. A thing I wanted to point out there is if you do +=, it acts the same way as an .extend() does. If you do z = z + and then another list, it actually creates a new list for you containing the result of that operation.

[03:13] Now you know that .append() will add a single element to the list regardless of data type, whereas .extend() will append each element from an iterable list.