1 00:00:07,970 --> 00:00:12,040 Let's see some useful methods that operate on lists. 2 00:00:12,040 --> 00:00:14,579 Some of these methods will mutate the list, 3 00:00:14,579 --> 00:00:17,800 some of them will just return a value. 4 00:00:18,860 --> 00:00:21,500 So, we've got a lot of things in this code. 5 00:00:21,500 --> 00:00:27,520 Let's run it using CodeLens so that we can step through it one step at a time. 6 00:00:31,700 --> 00:00:37,840 Our first command, just creates an empty list. 7 00:00:38,840 --> 00:00:46,360 The next thing calls the append method on it, that's line two. 8 00:00:47,000 --> 00:00:52,680 So, just as with elixir.forward we have mylist.append, 9 00:00:52,680 --> 00:00:58,740 that means, run the append method on mylist, 10 00:00:58,740 --> 00:01:01,620 which says, add a new item to the end. 11 00:01:01,620 --> 00:01:04,410 In this case, the item will be 12 00:01:04,410 --> 00:01:07,825 wherever we pass in as an argument it's going to be the value five. 13 00:01:07,825 --> 00:01:09,835 So, we start with an empty list. 14 00:01:09,835 --> 00:01:16,050 After we run this, we'll have a list containing one element, the value five. 15 00:01:20,020 --> 00:01:24,475 If I do it again, on line three, 16 00:01:24,475 --> 00:01:30,685 now the value 27 is going to be appended to the end of the list. 17 00:01:30,685 --> 00:01:33,135 So, we'll have five and 27. 18 00:01:33,135 --> 00:01:34,635 I can do it a third time, 19 00:01:34,635 --> 00:01:38,360 this time three, will be appended to the end of the list. 20 00:01:38,360 --> 00:01:47,365 It's helpful to notice here that the variable mylist is always bound to that list object. 21 00:01:47,365 --> 00:01:50,375 So, we have the arrow pointing to it. 22 00:01:50,375 --> 00:01:52,885 That never changes throughout all of this, 23 00:01:52,885 --> 00:01:56,270 what changes is the list itself, 24 00:01:56,270 --> 00:02:00,300 it's getting additional elements added to it. 25 00:02:03,070 --> 00:02:05,790 So, we get a fourth element there. 26 00:02:05,790 --> 00:02:07,560 If I now print it out, 27 00:02:07,560 --> 00:02:10,155 it'll show up in the output window. 28 00:02:10,155 --> 00:02:16,435 A second useful method is insert. 29 00:02:16,435 --> 00:02:18,960 Append always sticks an item at the end, 30 00:02:18,960 --> 00:02:22,600 insert, inserts the item wherever you tell it to. 31 00:02:22,600 --> 00:02:28,155 In this case, we're going to tell it to insert at position number one. 32 00:02:28,155 --> 00:02:33,590 So, it's going to go to position number one and it's going to insert it. 33 00:02:33,590 --> 00:02:38,930 It will not erase the current contents at position number one, 34 00:02:38,930 --> 00:02:41,540 that's what we had with assignment. 35 00:02:41,540 --> 00:02:43,565 Recall, we could do something like 36 00:02:43,565 --> 00:02:53,460 mylist square bracket one equals a new value. 37 00:02:53,460 --> 00:02:57,435 That would replace the 27 with 12, 38 00:02:57,435 --> 00:03:00,810 but that's now what happens with our insert command. 39 00:03:00,810 --> 00:03:09,810 Our insert command splices our new value in right before position 40 00:03:09,810 --> 00:03:15,450 one and we'll go 41 00:03:15,450 --> 00:03:20,650 forward and sure enough, 42 00:03:20,650 --> 00:03:25,290 the 12 has been inserted there between the five and the 27. 43 00:03:25,520 --> 00:03:31,300 Print that out and now the list has five items in it. 44 00:03:31,460 --> 00:03:37,930 Line 10 introduces a new method called count. 45 00:03:39,020 --> 00:03:43,295 The argument that you pass to count in this case,12, 46 00:03:43,295 --> 00:03:47,210 is not a position in the list, it's a value. 47 00:03:47,210 --> 00:03:51,860 The count operation is going go through all of the values in 48 00:03:51,860 --> 00:03:56,935 the list and count how many of them are equal to 12. 49 00:03:56,935 --> 00:04:00,735 So, in this case, the count will be two. 50 00:04:00,735 --> 00:04:10,260 That is something you did with an accumulation pattern in our previous lesson, 51 00:04:10,540 --> 00:04:14,035 but the count method just does it for you, 52 00:04:14,035 --> 00:04:18,080 counts how many times 12 appears in mylist. 53 00:04:18,080 --> 00:04:21,500 Index is a method, 54 00:04:21,500 --> 00:04:26,210 where we will pass 55 00:04:26,210 --> 00:04:33,820 in a value and it will tell us the position where that value can be found. 56 00:04:33,820 --> 00:04:39,680 So, we'll pass in value three and it'll tell us the index, 57 00:04:39,680 --> 00:04:43,685 the position where that three can be found, 58 00:04:43,685 --> 00:04:47,880 perhaps, somewhat confusingly, it's also three here. 59 00:04:47,880 --> 00:04:55,820 So, we're going to get back the value three. 60 00:04:58,320 --> 00:05:01,930 Print mylist.count to five, 61 00:05:01,930 --> 00:05:04,190 is going to look through and find all the places where 62 00:05:04,190 --> 00:05:07,240 five appears, there's only one of them. 63 00:05:07,240 --> 00:05:11,170 So, we'll get a count of one. 64 00:05:13,260 --> 00:05:24,640 Mylist.reverse, takes the order of the items in the list and changes them. 65 00:05:26,410 --> 00:05:34,595 So, the last becomes first and then we get the three, 66 00:05:34,595 --> 00:05:40,285 the 27, the 12 and the five. 67 00:05:40,285 --> 00:05:45,709 These values are actually going to go back into the original positions. 68 00:05:45,709 --> 00:05:47,840 So, the list itself is being changed. 69 00:05:47,840 --> 00:05:51,840 We're not making a new copy of the list. 70 00:05:59,570 --> 00:06:04,290 So, when I run that, it actually reverses the items, 71 00:06:04,290 --> 00:06:08,600 I printed it that come out in the opposite order that they were in before. 72 00:06:08,600 --> 00:06:10,580 Sort is another method, 73 00:06:10,580 --> 00:06:16,780 it's going to take the items in the list and just reorder them from smallest to biggest. 74 00:06:16,780 --> 00:06:20,515 So, we'll get three, 75 00:06:20,515 --> 00:06:28,170 then five,then 12 and another 12 and 27. 76 00:06:32,770 --> 00:06:35,325 So, sort and reverse, 77 00:06:35,325 --> 00:06:37,250 keep all the same items, 78 00:06:37,250 --> 00:06:41,560 but they rearrange them into a different order. 79 00:06:41,950 --> 00:06:50,665 Our next method, we see on line 21 is.remove. 80 00:06:50,665 --> 00:06:58,840 You've seen previously a deletion operation del, 81 00:06:58,880 --> 00:07:08,680 which takes a position 82 00:07:08,680 --> 00:07:11,995 and whatever item is at position one, 83 00:07:11,995 --> 00:07:16,805 would get deleted if I write del and mylist square bracket one. 84 00:07:16,805 --> 00:07:21,615 That remove, doesn't specify a position, 85 00:07:21,615 --> 00:07:24,360 it specifies a value. 86 00:07:24,360 --> 00:07:30,190 So, it's going to remove the five wherever it is. 87 00:07:36,410 --> 00:07:39,170 Now, the five is gone. 88 00:07:39,170 --> 00:07:42,320 There's one more operation for removing items, 89 00:07:42,320 --> 00:07:46,615 pop, it removes the last item from the list. 90 00:07:46,615 --> 00:07:50,565 So, in this case, when we pop mylist, 91 00:07:50,565 --> 00:07:56,300 we will get 27 and it actually returns that value. 92 00:07:56,910 --> 00:08:02,480 So, the list will be changed and we'll get the value 27 back. 93 00:08:02,480 --> 00:08:06,610 In this case, we've chosen to assign the results of 94 00:08:06,610 --> 00:08:12,110 the pop operation to a new variable called lastitem. 95 00:08:19,860 --> 00:08:28,460 Sure enough, lastitem now has the value 27. 96 00:08:34,340 --> 00:08:41,920 So, to summarize, you've learned here how to use several mutating operations, 97 00:08:41,920 --> 00:08:44,050 mutating methods on list, append, 98 00:08:44,050 --> 00:08:47,295 index, reverse, sort, remove and pop. 99 00:08:47,295 --> 00:08:50,970 But also, seen a couple of non-mutating methods, 100 00:08:50,970 --> 00:08:56,395 index and count that return values and pop, 101 00:08:56,395 --> 00:09:02,270 that was a mutating method that changed the list and also returned a value. 102 00:09:02,570 --> 00:09:08,380 Of all of these, I would say append is the method that's the most important. 103 00:09:08,380 --> 00:09:14,000 You'll be using append all of the time. See you next time.