Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Exploring Misconceptions About .append()

00:00 In this lesson, you’ll see two common misconceptions about the .append() method in the hopes that you won’t try to use .append() the wrong way. First is that .append() only adds a single element to a list.

00:15 Recall what you saw in the last lesson. If you create a list

00:26 and try to append another list to it,

00:33 you don’t get a new list with six elements, you get a new list with four elements, the last element being the list you just appended to the original list.

00:44 You’ll see another example. Here, you can create a variable for the original list, and what you want to append to it this time is a tuple, but it doesn’t matter.

01:00 .append() still treated the tuple as a single item and added that as a fifth item to the list. This doesn’t recreate the effect that the slice operation in the last lesson demonstrated. To do that, you’ll need to use the .extend() method.

01:17 This method adds elements from the argument provided as individual elements added onto the list. So here, if you again create a list,

01:33 and this time use .extend() with the list you want to add on, you indeed do get a new list with the six elements you were expecting.

01:51 And you can extend your list with a tuple and it will again have the same desired effect: a new list with individual elements for what you are extending on to the original list. So, use .extend(), not .append(), when you want to add multiple items to a list, which was the behavior you saw with the slice operation before.

02:19 Another thing to remember is that .append() doesn’t return a list to use. .append() does its work in place. That means it actually modifies the object you call it on. It doesn’t return a new or modified version of the list.

02:39 Technically, it returns None, but that basically means you shouldn’t plan on using it as if it’s going to return something meaningful. So, look at this. You can create a list and while calling .append() to it, save the return value to a variable.

02:59 But when you try to inspect that variable, you don’t see anything because it’s a reference to None, which basically means it’s not a reference to anything meaningful in this context. If you want to see the modified list, that’s still in x. x itself was modified.

03:20 The work was done in place. So again, don’t try to use any return value from a call to .append(). Don’t even save it. The list you use it on is being modified by adding the new element to the end of that same list. We call that doing the work in place.

03:41 Next, we’ll look at how .append() can be used inside loops to populate a list.

Become a Member to join the conversation.