Searching Lists3:32 with Jeremy McLain
It's often important to be able to search a collection for an item - if only just to remove it.
I mentioned in the previous video that there are a couple ways to remove items 0:00 from a list. 0:04 The remove at method can be used if we already know the index of the item 0:05 we want to remove, however we often won't know the precise index. 0:10 We need to find it first. 0:14 This brings up a very important aspect of collections that we should take a look at 0:16 and that's searching. 0:20 We often need to be able to find an item in the list 0:22 before we can retrieve it or remove it. 0:25 Sometimes we simply want to know if an item is in the list at all. 0:28 This also requires that we have the ability to search through all the items. 0:32 When it comes to finding and 0:37 removing items the list class provides methods that we can call to do this. 0:38 If we don't know the index of the item we want removed 0:44 then we can call the remove method with the actual item. 0:46 It's not uncommon to already have a reference to 0:50 the object that we want removed. 0:53 Let's take a look at our list of students again. 0:54 Let's say Frank has left the school. 1:00 But we don't know where in the list he is. 1:03 We can just call remove and pass in Frank. 1:06 So say students.Remove pass in Frank. 1:08 The remove method returns true if it found Frank and was able to remove it. 1:17 It will remove the first item it finds that matches the item we passed in and 1:23 then return. 1:28 So if we had more than one Frank in the list. 1:29 It would only remove the first one. 1:32 The remove method first searches the list for the item we're looking for. 1:34 It does this by calling the index of method. 1:38 Once it's found the index of the item it calls the remove 1:41 method to remove the item at that index. 1:44 The remove method first had to search the list for 1:47 the item before it could remove it. 1:50 The index of method which does this may potentially need to look at every item in 1:52 the list starting from the beginning in order to find the item it's looking for. 1:57 Luckily the item we were looking for was at the beginning of the list and 2:02 the list wasn't very long. 2:06 But consider the situation where the list is thousands of items long or 2:08 we need to search through the list many times. 2:13 Comparing strings is not a relatively fast operation. 2:16 Even if the strings themselves are short 2:20 it could take thousands of a second to find a string. 2:22 That's milliseconds. 2:25 Milliseconds doesn't seem like a long time and it isn't in most situations. 2:28 If we only need to find a few items 2:33 this is a perfectly fine way to go about searching a list. 2:35 However if we need to find ten or more items in a list of thousands 2:39 there are much more efficient ways to go about it. 2:44 When thinking about software performance we have to think in aggregate. 2:47 For example let's say our program is part of a web application that's used by 2:51 thousands of people simultaneously. 2:56 Now our string search that takes a thousandth of a second doesn't seem so 2:59 negligible anymore. 3:03 It's only one operation in hundreds of thousands of other operations 3:05 that all need to finish in mere seconds. 3:09 Searching through a list of items one at a time 3:13 is the slowest search method that can be used. 3:15 The list collection type provides faster ways to find items in a list but 3:18 there's a caveat. 3:23 In order to find items faster the list must first be sorted. 3:24 In the next video let's see how to sort a list. 3:29
You need to sign up for Treehouse in order to download course files.Sign up