1 00:00:00,710 --> 00:00:04,380 I mentioned in the previous video that there are a couple ways to remove items 2 00:00:04,380 --> 00:00:05,598 from a list. 3 00:00:05,598 --> 00:00:10,010 The remove at method can be used if we already know the index of the item 4 00:00:10,010 --> 00:00:14,630 we want to remove, however we often won't know the precise index. 5 00:00:14,630 --> 00:00:16,730 We need to find it first. 6 00:00:16,730 --> 00:00:20,890 This brings up a very important aspect of collections that we should take a look at 7 00:00:20,890 --> 00:00:22,690 and that's searching. 8 00:00:22,690 --> 00:00:25,820 We often need to be able to find an item in the list 9 00:00:25,820 --> 00:00:28,370 before we can retrieve it or remove it. 10 00:00:28,370 --> 00:00:32,710 Sometimes we simply want to know if an item is in the list at all. 11 00:00:32,710 --> 00:00:36,380 This also requires that we have the ability to search through all the items. 12 00:00:37,470 --> 00:00:38,760 When it comes to finding and 13 00:00:38,760 --> 00:00:42,980 removing items the list class provides methods that we can call to do this. 14 00:00:44,090 --> 00:00:46,960 If we don't know the index of the item we want removed 15 00:00:46,960 --> 00:00:50,150 then we can call the remove method with the actual item. 16 00:00:50,150 --> 00:00:53,080 It's not uncommon to already have a reference to 17 00:00:53,080 --> 00:00:54,960 the object that we want removed. 18 00:00:54,960 --> 00:00:57,260 Let's take a look at our list of students again. 19 00:01:00,146 --> 00:01:03,090 Let's say Frank has left the school. 20 00:01:03,090 --> 00:01:06,290 But we don't know where in the list he is. 21 00:01:06,290 --> 00:01:08,944 We can just call remove and pass in Frank. 22 00:01:08,944 --> 00:01:17,620 So say students.Remove pass in Frank. 23 00:01:17,620 --> 00:01:23,450 The remove method returns true if it found Frank and was able to remove it. 24 00:01:23,450 --> 00:01:28,110 It will remove the first item it finds that matches the item we passed in and 25 00:01:28,110 --> 00:01:29,290 then return. 26 00:01:29,290 --> 00:01:32,440 So if we had more than one Frank in the list. 27 00:01:32,440 --> 00:01:34,630 It would only remove the first one. 28 00:01:34,630 --> 00:01:38,780 The remove method first searches the list for the item we're looking for. 29 00:01:38,780 --> 00:01:41,590 It does this by calling the index of method. 30 00:01:41,590 --> 00:01:44,680 Once it's found the index of the item it calls the remove 31 00:01:44,680 --> 00:01:47,240 method to remove the item at that index. 32 00:01:47,240 --> 00:01:50,390 The remove method first had to search the list for 33 00:01:50,390 --> 00:01:52,600 the item before it could remove it. 34 00:01:52,600 --> 00:01:57,520 The index of method which does this may potentially need to look at every item in 35 00:01:57,520 --> 00:02:01,370 the list starting from the beginning in order to find the item it's looking for. 36 00:02:02,900 --> 00:02:06,970 Luckily the item we were looking for was at the beginning of the list and 37 00:02:06,970 --> 00:02:08,720 the list wasn't very long. 38 00:02:08,720 --> 00:02:13,260 But consider the situation where the list is thousands of items long or 39 00:02:13,260 --> 00:02:16,130 we need to search through the list many times. 40 00:02:16,130 --> 00:02:20,100 Comparing strings is not a relatively fast operation. 41 00:02:20,100 --> 00:02:22,780 Even if the strings themselves are short 42 00:02:22,780 --> 00:02:25,980 it could take thousands of a second to find a string. 43 00:02:25,980 --> 00:02:27,180 That's milliseconds. 44 00:02:28,270 --> 00:02:33,210 Milliseconds doesn't seem like a long time and it isn't in most situations. 45 00:02:33,210 --> 00:02:35,830 If we only need to find a few items 46 00:02:35,830 --> 00:02:39,500 this is a perfectly fine way to go about searching a list. 47 00:02:39,500 --> 00:02:44,210 However if we need to find ten or more items in a list of thousands 48 00:02:44,210 --> 00:02:46,350 there are much more efficient ways to go about it. 49 00:02:47,630 --> 00:02:51,700 When thinking about software performance we have to think in aggregate. 50 00:02:51,700 --> 00:02:56,410 For example let's say our program is part of a web application that's used by 51 00:02:56,410 --> 00:02:59,310 thousands of people simultaneously. 52 00:02:59,310 --> 00:03:03,270 Now our string search that takes a thousandth of a second doesn't seem so 53 00:03:03,270 --> 00:03:05,020 negligible anymore. 54 00:03:05,020 --> 00:03:09,770 It's only one operation in hundreds of thousands of other operations 55 00:03:09,770 --> 00:03:11,910 that all need to finish in mere seconds. 56 00:03:13,040 --> 00:03:15,710 Searching through a list of items one at a time 57 00:03:15,710 --> 00:03:18,590 is the slowest search method that can be used. 58 00:03:18,590 --> 00:03:23,050 The list collection type provides faster ways to find items in a list but 59 00:03:23,050 --> 00:03:24,500 there's a caveat. 60 00:03:24,500 --> 00:03:29,050 In order to find items faster the list must first be sorted. 61 00:03:29,050 --> 00:03:32,030 In the next video let's see how to sort a list.