4.3 Iterating Data Structures
With these new iterable data structures at our disposal, we're ready to learn some new and slightly more advanced methods of processing structured data.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
4.3 Iterating Data Structures
In the previous lesson, we spent some time going through my favorite collection, basically of all time, regardless of language or platform or what have you. And that is of the dictionary. Now, a dictionary is a very powerful collection that you can use to store collections of key value pairs, where you are able to look up certain values within this dictionary based on unique look up keys. So once we've been able to do that, once we've created these dictionaries, how do we iterate over them to maybe? Do some sort of processing of all the values in the dictionary, or maybe look up and do something with all the keys or the values or what have you. What's an easy way for us to be able to go through that? And actually you're going to see, once I show you this little technique, that you can actually use this to maybe even more effectively and efficiently loop through some of the other collections, such as lists that we talked about previously. So let's go ahead, and start by creating a very simple little application here. I'm gonna create a very simple dictionary. And I'm going to set the sequel to open and closed parentheses, because remember that is going to denote the fact that we're dealing with a dictionary. And then I can start to specify here comma separated, key value pairs. So, just kind of going along the same example that we were doing before when I was creating a dictionary of people and their ages. So, let's just say here that we had Derek, and Derek is going to be 30. And we're gonna have Katie, and Katie is gonna be 27. And maybe we have Bobby, and Bobby is going to be 12. And then maybe we have Andrea. And Andrea is going to be ten. So, there's a basic dictionary. We have four key value pairs here. And now, let's say we get to a point in our application where we want to iterate over them. Well, now remember, we're dealing with basically a collection of little mini objects, if you want to think of it that way, like key value pairs. So, if we wanted to iterate over them, and just maybe print them out let's without jumping into anything new, let's see how we might do that given the information we already know. Well, let's just say we wanted to go through a for loop, and we wanted to loop over maybe all the pairs. So, for each pair that we find that is in the dictionary, I want to do something, I wanna print it out. So, I want to print pair. So, let's see what that's going to do for us. So, I'll go ahead and save that and I'm going to save this as a dictionary_loop.py, and so now we have this, and so I'll go ahead and I'll come into my shell and I'll say pythondictionary_loop, and lets run that well it kind of worked we got our value here for each of the key value pairs within our dictionary, and I was able to print them out onto the screen, so I got the names in essence I got the keys out of the dictionary, but where are the values? How do we get the values? Well, interestingly enough, when you start to deal with for loops and dictionaries in this construct here, dictionary is going to implicitly say okay, in this loop, you want the keys. So, I'm going to give you the keys. And now, you can do whatever you want with them. Well, that's a little bit limiting. What we could maybe do is we could get a little bit more creative and we could try to say, all right I wanna print. I wanna print pair, so that's going to be the key, and then maybe I wanna print the values so maybe I could do something like this I could say dictionary and I could do pair as well so we can maybe use that key to look up that value in the dictionary, so let's go ahead and save that, and let's go ahead and run our application again on. So, there it does kind of work. So, I can do something like this but I don't know about you, but if I were to look at this. This is kind of seems a little bit confusing, so we have for a pair in this dictionary, and then I'm referring to that dictionary using that key. But this isn't really a pair this is a key. So maybe, I'd have to rethink some of my naming conventions or things of that nature. But the nice thing is when it comes to collections like this, specifically key value pairs in a dictionary. There's another construct that you can use within the four loop to maybe make it seem a little bit more intuitive. So what I can do now, is I can say instead of just pair, you can call these variables anything. Remember, in any of these loops when you're referring to an instance of something within a collection you can give it any variable name. So, let's just say for argument's sake. I say I want the key and the value. So here, I'm going to specify multiple variable names, one for the key and one for the value. Now remember, these can be anything, this can be high and low, inside, outside, car and duck, it doesn't really make any difference. I'm just specifying them this way to prove a point. Once I've created this comma separated list of variable names, now with my dictionary there's a special function that has been added to this dictionary type called items. And what items is going to do is it's going to give all of the, excuse me, that's items. It's going to give me back within with each iteration of this four loop, the key value pairs separated out into two values which are going to ultimately be placed Into these two variable names. So then, we'll have a key and a value so now I could do something like this I could say key value, so I'll go ahead and save that and I'll rerun my application and I get a failure here because this was actually val not value, so we'll save that and there we go there are our keys and our values again, so there we've done a little bit of a cleanup, a nice little, as I said, construct in the Python language in the for loop to be able to put together a collection this way. So, this kind of brings up an interesting concept or an interesting thought, if I can do something like this with a dictionary that has keys and values or basically multiple pieces of information for each item in the collection. Can I do that with other collection types as well? So, say maybe a list. Well, the answer to that question is actually yes. So, let's go ahead and take a look at another example here. So, this time let's create a basic list. And this list is going to be equal to just a list of numbers, so we'll say 1, 5, 9, 15, 27 and 30. Something like that. So, just some arbitrary numbers that are being saved into a list. Now, let's see what would happen if I were to do something similar to what I did for the key value pairs? So, this time we'll create another for loop. And actually, I'll throw in here another print just to print out an empty space between the top for loop and the bottom for loop, but this time let's do something similar. So, let's just say for argument's sake, I did something silly like naming the first variable here index, and the second one value. And so, we'll say for index and value in now what I want to do is something similar to list so let's go ahead and see what would happen here if I did something like print index and value, so I'll go ahead and save that, and I'll go ahead and clear out my screen here. And I will rerun my application. And we got to the first part, but this business down here at the end didn't seem to work very well. I'm trying to get two values here. But line eleven didn't seem to think that the list here, or the value coming back from each value in the list is not iterable. So iterable, what exactly does that mean? We'll, interval is just a fancy way of saying it's something that can be split out into two values that has an index and a value, although, there's another special way that we can do something similar to our items function here on a dictionary is we can take a collection like list, and we can enumerate it or transform it into something that is iterable. And so to do that, in Python we simply say that I want to enumerate this list. And by using this little helper function, everything that comes out of this list now is going to be iterable as you see here. And so, I'm going to start getting multiple values here. So, that's one save this and see what we get now. So now, we've got everything at the top we were expecting, and so what did we get on the bottom? So we are actually getting, when we enumerate this list the values that come out are iterable values, that have both an index and a value within that list. So it index zero, we have the number one at index one we have five and so on and so fourth, up to index five having a value of 30 so there you have it now we have a new construct with the Python language namely for four loops and being able to iterate over a value that's in a collection whether it be a dictionary, or a list and being able to know whether being able to know what value coming out of it is the key and a value if we're talking about it a dictionary, or an index and a value for talking about another collection such as a list. So now, that we've gone through the process of some more data structures within Python and being able to loop through them. In the next lesson, let's build a little bit of an application to then demonstrate some of these new skills that we have.