WEBVTT 00:00:00.000 --> 00:00:04.000 [Narrator] I hope you're getting a sense that lists are very powerful. 00:00:04.000 --> 00:00:07.000 That by using mutation, by having lists that could contain other lists 00:00:07.000 --> 00:00:10.000 or any other kind of data we want, 00:00:10.000 --> 00:00:14.000 we can build very complex data structures, and we can do lots of interesting things. 00:00:14.000 --> 00:00:16.000 I'm going to introduce 1 more list operation, 00:00:16.000 --> 00:00:20.000 which will allow us to add a new element at the end of the list. 00:00:20.000 --> 00:00:24.000 We've seen that we can use lists to store complex data, 00:00:24.000 --> 00:00:27.000 that elements of the list can be any type we want including other lists, 00:00:27.000 --> 00:00:30.000 and we've seen that we can use mutation to change the value of a list, 00:00:30.000 --> 00:00:35.000 and that mutation is visible through any reference to the same list object. 00:00:35.000 --> 00:00:39.000 Now I'm going to introduce some other list operations. 00:00:39.000 --> 00:00:44.000 The first one is append, and append is like a procedure but it's a method, 00:00:44.000 --> 00:00:47.000 so we use it similar to the way we use to find on strings. 00:00:47.000 --> 00:00:52.000 We'll have a list first, then a dot followed by append, 00:00:52.000 --> 00:00:55.000 and what we pass in is the element we want to append to the list. 00:00:55.000 --> 00:00:59.000 Append will add a new element to the end of a list, 00:00:59.000 --> 00:01:01.000 and the important thing about append is that it's mutating 00:01:01.000 --> 00:01:04.000 the list that its invoked on. 00:01:04.000 --> 00:01:08.000 It's not creating a new list; it's mutating the old list. 00:01:08.000 --> 00:01:12.000 As an example of the use of append let's assume that instead of 00:01:12.000 --> 00:01:16.000 replacing curly in the 3 stooges, we want to end up with 4 stooges. 00:01:16.000 --> 00:01:19.000 We'll add Shemp and add the other 3 as they are. 00:01:19.000 --> 00:01:24.000 So what we want to do is to append Shemp at the end of the list we have. 00:01:24.000 --> 00:01:28.000 We would do that by invoking append on the stooges, 00:01:28.000 --> 00:01:31.000 passing in the string Shemp as the input. 00:01:31.000 --> 00:01:35.000 Here's what happens after the first assignment, 00:01:35.000 --> 00:01:39.000 the name stooges refers to the list containing the 3 elements, 00:01:39.000 --> 00:01:41.000 Moe, Larry, and Curly. 00:01:41.000 --> 00:01:46.000 When we invoke append it modifies that object, 00:01:46.000 --> 00:01:48.000 adding a new element to it. 00:01:48.000 --> 00:01:54.000 After the append, the list that stooges refers to now has 4 elements. 00:01:54.000 --> 00:01:56.000 We have not created a new list. 00:01:56.000 --> 00:01:59.000 Note that there's no assignment from the result of append. 00:01:59.000 --> 00:02:03.000 What we've done is modify the value that stooges refers to 00:02:03.000 --> 00:02:06.000 by adding a new element to it.