Python Fundamentals

# List Lookups

## Nina Zakharenko

Microsoft

### Check out a free preview of the full Python Fundamentals course

The "List Lookups" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to determine if specific objects are in a list, the index of the first time the object is found in a list, and the number of times an object appears in a list.

Preview
Close
Get \$100 Off
Get \$100 Off!

### Transcript from the "List Lookups" Lesson

[00:00:00]
>> Nina Zakharenko: If we wanna look for items, we have a few ways of doing that. But you have to remember that looking for an item in a list is actually a really slow operation. That's because under the hood, each value is checked to be like are you the right one, are you the right one?

[00:00:17]
No, okay, let me look at the next one, are you the right one, right? And so for a large list, that can take some time.
>> Nina Zakharenko: So to check if an item is in the list and at which position there's a few ways of doing that. So I'm gonna make a new list of names, I'm gonna put four items in it

[00:00:46]
>> Nina Zakharenko: Now, purposefully the last item that I'm putting in this list is the same as the first item. So if you're making your own list of names, make sure the pattern that same kind of caveat.
>> Nina Zakharenko: We can use the in keyword to determine if a value or an item is present in the list or not.

[00:01:10]
So I can say here's the value of a variable and then I can use the in keyword and ask is Rose in names and I'll get back false. If I try the same thing with Nina, will get back true. So true and false are Python's Boolian values, right?

[00:01:34]
They're built in types, they're always available, right? So when I asked if Nina was in names, Python said true, which means yes. When I asked if Rose was in names, Python said false, which means no.
>> Nina Zakharenko: You can also use the index method on the list to find the first index of a potential match.

[00:02:02]
So if I call names.index("Max"), I'm gonna get the index of 1. If I call names.index("Nina"), what am I gonna get, what?
>> Student: One, or zero, yeah I'm sorry.
>> Nina Zakharenko: Zero, that's right, zero. It's only in the first result. Even though Nina is repeated again at the end of the list, using index will only give me the first result.

[00:02:36]
If you want to figure out how many times an item appears on a list, you can use the count method. So names.count of Max will be 1. Names.count of Nina will be what? 2, okay cool.
>> Nina Zakharenko: So I already showed you how to update items at a position, right?

[00:03:01]
If I wanted to change the very first item in that list to be, I already showed you how to update items at a position, right? If I wanted to changed the very first item in that list to be at Jimmy, I can just do that by using the index and if we take a look we'll see our new list, right?

[00:03:27]
>> Nina Zakharenko: You can also use this in combination with .index to find the value that you wanna change. So let's say I want to replace Phillip with Floyd. I can say the position that I want is names.index of Phillip. And then, I can update my names list at the returned position, oops, Floyd one l or two l's?

[00:04:03]
>> Student: One
>> Nina Zakharenko: One l, okay, oops, okay, I got a name error. The name Floyd is not defined, why did that happen?
>> Student: No quotes around it.
>> Nina Zakharenko: Right, I forgot to put quotes around it. So Python thinks that it's a variable that I haven't declared, but really I just forgot my quotes.

[00:04:24]
Okay, so taking a look at our names now we'll see that Philip has been replaced with Floyd at the position that we found.
>> Nina Zakharenko: And just like we saw earlier, so I have our list of names right the length of the names is four. If I tried to update an item at index 4, it's not a valid index, right?

[00:04:52]
Our index that we count from starts at 0.
>> Nina Zakharenko: I'm gonna get an error, an index error that the list assignment index is out of range.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses