In this video, we're going to look at how to search list to figure out if a particular item is actually in a list or not. Again, things are probably going to look pretty similar to what we did with strings, but I'm going to introduce a few different things as well. So here we have toys, which is a list of strings which are names of different toys, and we're going to search within that list. If you remember when we talked about searching with strings, there was an index method on strings that would return the index of a particular substring. Well, lists have a similar method called index as well that returns the index of a particular item if it exist in the list. So let's try it. We're going to look for both legos and blocks, and let's see what happens. We get back five and zero. Well, it says that the index of legos is five, and if you look at the list, this makes sense. Legos is the sixth element of the list, so five is the correct index here. And blocks does appear as the first element of the list, so zero is also the correct index here. But notice, however, that blocks appears multiple times in the list. We know the index gives us back the index of the first occurrence of that item in the list, but what if I want the last one? If we remember, strings had this rindex method that would return us the index of the last occurrence of the list. And if we run it here, list do not have this rindex method. So it's not so easy to find the last occurrence of an element within a list. Right? Now if you remember what happens in strings if I use the index method and I look for something that isn't there, it throws an error. Let's check with lists. Okay, it also throws an error, says video game is not in the list. Unfortunately, unlike strings, lists do not have a find method that conveniently would return minus one as an index if it wasn't there. So lists do not have an equivalently easy way to search for the index of an item in a list if you don't know if it's going to be there. If it's not there, you're going to get an error, which may be okay, but if that's not what you want, you have to be a little bit smarter about how you do things. If we don't want to have to figure out how to deal with the error that index can throw if an item is not in a list, how do we check if something is in a list when we're not sure? Well, conveniently, Python has something called the in operator. And the in operator is a binary operator that takes two things: first, the item that you're looking for; and second, the sequence that you're looking to see if that item is in. So you can see here we have legos in toys, the string legos in toys. This says if legos is in toys, evaluate to true. If it's not in toys, evaluate to false. We can basically understand that with the sort of English sense of the meaning of the phrase here, legos in toys. And let's try it, let's see what it says. That evaluates to true. So let's try again. Blocks in toys and video game in toys. OK, so we know that blocks does appear in toys multiple times and video games does not, so let's see what happens. Blocks is in toys, so it evaluates to true. Video game is not in toys, so the in operator evaluates to false in that case. Conveniently, maybe I want to check the other way – I can do not in. All right, so we have teddy bear not in toys and dice not in toys. Well, teddy bear is in toys so I'd expect if I'm asking if it's not in toys to get back false, and dice is not there so I do expect not in toys to evaluate to true. Let's see if that happens. And in fact, it does. Right? So I can use in and not in to check for membership inside of a sequence, in particular, in this case, a list. I can also figure out how many times a particular item occurs in a list. Let's have a count method, just like strings do. And it works the same way again, you give in an item and it will return how many times that item appears in the list. So if I ask it to count slinky, I get one, and if I ask it to count the number of times blocks occurs, I get two. And again, if you ask it to count for something that was not in the list, the answer would be zero. Searching for items in a list is pretty similar to searching for substrings within a string, but it's not exactly the same. There are a few extra methods that strings provide to make it a little bit more convenient that lists don't have. Sometimes it's appropriate to just try those, see what methods does this have. You can either read the documentation or just try it out and see what happens. But on the whole, they are very similar. The index methods work exactly the same. You can actually use the in and not in operator also on strings and other sequence types, and the count method works pretty much exactly the same. So, once you're familiar with how to search with one sequence type, you pretty much get the idea in the other. There might be slight variations. But, again, reading documentation or experimenting should help you figure it out.