 Hello, this is Christian. So in this lesson, we're going to write a program in Python to build a list of dictionaries with the keys first night, last night, email, and company salary, just like you see this table here. So we're going to read this file from the employees.csv file and build a list of dictionaries. And then you're going to search in the list. And once you find a matching substring of the first name field, then we're going to build another list called the result list and print that out. And when we search that key term, it should be case insensitive. So as you can see here, capital Ds should match all lower case. Like here as well, lower case dy also matches capital dy and so forth here. All right? So let's go and take a look. Now the CSV file, if you download that and open it, it will look something like this. It's composed of about, I think, 1,000 records. If you go all the way to the bottom, 1,000 records here. And it has about five fields, like the first name, last name, email address, the company they work for, and their salary. So that is the idea. So the concept is, the first part is to read this text file line by line, break it up, and build a dictionary out of this line. So each of these is a record. And we call it a dictionary in Python and build a list of them. But the one we're going to do is something a little bit different, but the idea will be the same. So here in the spider ID, I have a main program called main.py. And then I have a CSV file called people.csv and the models folder. And there is the same level. So I'm going to make sure I load this file correctly. This file has, it shouldn't have that many. It's about 50, yeah, 50 records. If I expand this a little bit, so we have 50 records. It's a 51. That's because we have the first row as the headings. So we've got to remove these headings, of course. But it has about one, two, three, four, five, six fields. And this is the name of those fields or those keys. So I'm going to copy this one here and let's save this for now. And we'll go here, put some notes in here. So we have some information. Okay, so this will be my keys. Okay, that's the key fields. So the idea is what are we trying to do, right? Excuse me. So when we read every single line, we're going to build a dictionary and it will look something like this, okay? So this is the dictionary. We're going to have the ID of whatever that is, like one, zero, zero, one, for example. That, and then we have the first. I'll just call it first instead of first name. And then we'll give it a name like, you know, Jake. And then last and so forth, right? For example, okay, we're going to build that dictionary and eventually we're going to have a bunch of them. So we're going to put, let me just put example, we have two of them. We're going to have two of those, separate with a comma, and then we're going to wrap that with a square bracket because you're going to build a list of dictionaries, okay? And then we're going to assign that to another list, like something like that. So this is the idea, right? This is our goal for part one, okay? So let's do that. So first of all, I'm going to open this text file and we use the open with open command. So we don't have to close it automatically. It's inside the models folder. So actually you're going to put it dot slash just to make sure you're in the current directory, which is in the main directory here. And I'm going to the models folder and then the file is called people.csv. Okay, you don't want to put it, you can put it R if you want to. You don't have to, it's optional, but I'll put that anyway. It just means for reading purposes. And we're going to create an alias as a file, okay? So the first thing I'm going to do is read the lines. You have a lot of options here. You can read line by line as you read through the file, or you can read all the lines together into a single object, in this case a list object, and then you process the list. So entirely up to you. For my example, I'm going to read everything to a lines first. So I'll put like a lines, I guess you call lines called records maybe, doesn't whatever we call it, records, everything from the file dot read lines. Okay, notice the S here. Okay, without the S it's different. The S means all the lines and I'm going to break it into a single line. So what we have inside the record is really a list of lines, right? And just to make sure what we have in here, I'm going to close this side over here so I have more room, okay? We can run this now. And just to see, make sure that it's working. So I'm going to run this now. And then over here, you see that the records is a list of 50 people. If I open that up, just double click on it, you're going to see that inside here we have from zero up to 49, right? 50 people. And each of these record is just a text, right? We notice that the type is a string. So we're going to extract each of these lines, each of these cells, and then we're going to split it and create another list of fields so we can access their information directly from that list. Because right now it's a string. It's not really useful, okay? So that's the idea. So now I'm going to go ahead and run a loop through each record. So we can do something like for each record. And the records, okay? Like that. And then because I'm pulling the record right away, I'm not using the index, okay? You could also use the range function if you want, go and do the index, but this is a better approach, okay? So for every record, I'm going to break it up, split it into fields. And I'm going to go through the record. That is the first line. And if you just do a split like this, okay? Remember that if you do a split, you do at the comma, right? At the comma as a separator here. If you do that, then there might be something you want to clear something. Let me see. If I say that, I'm going to show you. I'll break that for now. Okay, so run this. And so you look at the record in the fields up here. In order to break it up. And it's in the, so everything is in their own place, right? So now the fields has all the fields that I need. This is the ID name, first and last name and so forth. Great, perfect. If you go down here and type in the console fields, and you're going to see that the very last field, which is the language field has a slash n. This is the carriage return, right? We don't want to leave that there, it looks ugly. And so you have to strip that off before you split it, okay? So before I split it, I want to do is a strip first and then that split. Okay, so you can chain these methods together. Just wait until you do it. You split, strip first before you do a split. Otherwise it doesn't work, all right? You don't have to put it in here. That means it's the same thing as a white space or a carriage return. By default, it's both, so just do that. And then now, again, if I save it, run it. You're going to see it's updated. If I type my fields now, as you can see, slash n is no longer there, right? Perfect. Then now I can start building my dictionaries, okay? So what I'm going to do here, I'm going to build a dictionary. And first I'm going to build a temporary person first or record first. So because they're people that put a person, it's going to be an empty person for empty object or dictionary. And then I'm going to plug in all the data, all the key value pairs, okay? So I'm going to do somebody person of the ID. And again, these IDs here are these fields up here, okay? First name, last name, title, country, language, okay? So ID will be equal to the fields of zero, okay? That is the ID field. And then I'm going to copy this and do it a couple of times. Make it a little bit faster, I guess. First, last, I forgot, I forgot, I forgot. Short memory, last title, country, language, okay? So this is the first, here is the last, this is the title, country, and the language, okay? Great. And then just update this to one, two, three, four and five, perfect. So once you've built this person, then you want to add it to a list of people, right? If you don't do that, then notice every cycle through we've key recreating these persons, all the data actually overwritten and we don't have the collection of the people. So you want to store that to or push that to a list of people. But you do that by going out here, right? Outside here, whether it's out here or above here, it doesn't matter. So maybe I'll put it, that's what up here, for now it's okay. If we put here people list, underscore list, it's an empty list, okay? Once I find that, once I've built that person dictionary, I'm going to push it or add it, add it, append to list. Okay, and it cycles through the whole records and then it would do that all for us. And when we're done, then the people list will be filled with 50 records. So let's run and test. So here we go, there is the people list and that is what we want. So now we have a dictionary of 50 people, okay? And to access it, we can go ahead and try here, people underscore list of zero. That's the first person. I want to get the country name. So again, just up arrow, type in the country and that will give you the name of the country for that person, great. So now our dictionary has been built and a list is now done. So this is now finished with part one, right? Let's move to part two. Part two is the search, therefore two, search, okay? And I put notes here, for one, a list of dictionaries. The exercise I ask you to choose one field and we'll do that, but you can choose all the fields if you want to, it doesn't really matter. We'll just do one, okay? So when we search, we're going to prompt the user to enter a term, so we call it term and we then put enter, enter a search term, okay? Just like that and we'll force it to be lowercase, okay? We'll accept lowercase. So now we're going to build a new result set. So here result, result set will list. That's the one we'll call it list. Because it's a list, we'll keep all the result into that list, okay, if you find any. So then we're going to look inside that people list, go through every individual in the list and match one of the fields with the term here, right? So we're gonna go ahead and say for every person in the people list, okay? Now we got the first person that looks like this, but now we got this object. We're gonna pair the term against, let's just say we go with the country, okay? Go with the country instead. So then we're going to do something like if the term is in the person of the country, okay? Like that, right? If that is true, if that matches, that means this term is a substring of the country. So if I enter like OAT, it should match this. If I type the CR, it should match this, right? Remember capital C here is not the same as lower case C. So we also want to avoid this. That means you want this to be case insensitive. So I'm gonna force everything to be lower case to match our input, right? So here before I do this, I'll say country.lower, okay? If that is in there, then we're gonna push this person or append this person to our result list. That's what we do. The result on the school list that append this person, okay? And then we keep doing that until all the list, right? Because it's possible that we might have two or more people from the country or Croatia or whatever country it is. That is the key. And once it's done, then we go ahead and print it up. So here we can print it out. So here will be print. I'll use it for loop. I'll just print maybe just the ID and the country name, okay? So for every, I don't know, I guess person again and the result set, result list. I'm gonna print, let's see. I'm gonna print the, I'll use this one here, ID, ID, which is person of ID. I'll make it simple. This is maybe too many things, I'll just make it easy. Here will be person of ID, okay? And then it'll follow by colon and then follow by the person of the country, okay? And then we're just done here, all right? So let's see, and this one works. Go ahead and run it. Okay, we've built a dictionary. I was asking for a term, I'll put CRO. And there it is. So we have two people that are from Croatia. So if you run it again, press the up arrow key, hit enter. Let's look for CZ, okay? From one from the Czech Republic. And again, we put here US and we have one from Russia and try again, if you put UN, you get it from the United States United Kingdom. Okay, perfect. So this is basically a little search engine that can search in our dictionary and match any of these fields. If you want to go and search for a different field instead of the country, you just put them here, okay? So that's for that one. What if you want to search for all the fields in here? I want to search for maybe not the ID, but first, last, middle, country, language, anything matches in there will be pulled. How would you do that? And that is quite simple, right? So in here, you could do the, or if you want to, like person in a country, lower, it's a lot of work, you can do this, right? Say, or, and I'll paste that in here. Let me make it maximized here. Or in a person of the first, or in the new key going to all those five fields. That will work too, just a lot of or statements. So instead of doing this way, you could do something like this. You could say for every value, value, and the person of values, right? Because this is a dictionary, right? Person that values or return all the values for us to look at. And just show you, right? If you go back here again, so we see the, for example, the person, right? Person is the object. If you want to get all the keys, is person.keys. Key function, turn all the keys, I want the values, you go with the values. See, right? So get all the values. And we're just going to look at the, all the values, even the key two, ID two, it doesn't really matter. So for any values, and you want to check. So back to this term again, I'm going to push that over. If term is in the value to the lower case, then push that to the list, okay? And we can omit the first one, but that's okay. Because all the ID is one away. So in this case, it search for ID two. And I'm just going to leave it at this. But now we search all the fields, okay? So let's see what happens over here. If I go and clear my list and run this now, we look at the term for, if I look for, let's say ID of 1, 0, 0, 7, right? If I took 1, 0, 0, 7, this matches the ID, as you can see here is from Germany. I do it again. If I search for the Brazil, right? So put the IEL here, you search finds the Brazil. If I search for, you know, the accountant, just put like T-A-N-T here, here, here we go. Even though you can see that the country doesn't have it, but it matches the name of that, you know, title. It's 1, 0, 18, it's right here. Okay, so that is pretty much it. And so once you have this working, right? I often ask you to, you know, move this out into functions. Okay, I highly recommend you do that. So you, you know, if you learn to do it, you learn better. So I leave that to you and you can solve that by moving this into a function and call those functions. Okay, well, thank you so much. Having any questions, please let me know.