Dictionaries map keys to values, and very frequently, you just want to look up in the dictionary what value is mapped to a particular key. But there are also many situations in which you might want to look at all of the contents of the dictionary. So how do we do that? Well, we can iterate over dictionaries just like we can iterate over other types of collections in Python. So in this video, we're going to look at several of the different ways in which we can iterate over dictionaries. Let's get started. So in order to iterate over a dictionary, we actually need a dictionary to iterate over. So I've created this dictionary here, capitals, which maps countries to their capital cities. Okay, so I have a few countries here and I have their capital cities. There are a couple different ways in which you can iterate over a dictionary. So let's start with directly iterating over the dictionary. So this looks familiar probably, the syntax. I have for country in capitals. Well, that looks almost exactly like the way that we iterated over lists, for example. But what's going to happen there? Okay, the question is what do I get back? Well, when you do direct iteration like this, it iterates over the keys in the dictionary. So country is going to take on the value of each key that is in the dictionary, right? And you'll notice here, I can then use that to index in the dictionary if I'd like the value. So I'm just going to print out all the capital cities in their countries. Let's try it out. All right. And so you can see, it goes through the dictionary. It gives me the keys and then it prints things out. Now, I would like to point out, when you do this and iterate over a dictionary, dictionaries do not necessarily have any ordering to them. So you may get the keys back in any order, not necessarily the order in which they were put in or the order in which they were written if you have a dictionary literal like I do. You'll notice in this case though, it did come out exactly the way I put it in, but you cannot rely on that. That's not guaranteed. Now, Python also allows me to be more explicit about the fact that I want to iterate over the keys in the dictionary. Dictionaries have a method called keys, which returns a sequence of the keys. And so you can see here, instead of just saying for country in capitals, I can say for country in capitals.keys, and it's a lot more obvious than I want to iterate over the keys, but it's exactly the same thing effectively where country will take on each key in the dictionary in turn, I can use it to look up the value if I like. So let's run this, and we get the exact same results here. Again, there's no guarantee about the order of the keys. Okay, so you can't rely anymore on the ordering when you use the keys method than you could when you just iterate directly over the dictionary. If I don't actually care about the keys in the dictionary, I could just iterate directly over the values as well, so Python has that capability, where dictionaries have a method called values. Right? And so you can see here, I say for city in capitals.values, and now, the city will take on each value in the dictionary in turn. Here, you definitely are not guaranteed any particular order and you don't necessarily know which key that value belong to. You're just going to get the values in sequence. So let's see what are all the capital cities that are contained within my dictionary. And here, I end up with being able to print out each city, not necessarily knowing what country they belong to. Now, if during your iteration, you want both the keys and the values, you can do what we've shown before, where you can just iterate over the keys and then access the dictionary to get the values, or you can iterate directly over the key value pairs. So dictionaries also have a method called items, which actually returns the sequence of tuples here. Okay, and so we get both the country and the city. So I can say for country city in capitals.items. And then each time through the loop, I will get the associated key value pair of country and city. And now, I can just use them directly, and I don't have to take the country and look it up in the capitals dictionary to find out what its capital is. So it's not, and you see yet again, we're able to print out all of the capital cities and their associated countries. Now, finally, sometimes you just want to check whether or not a particular key is in a dictionary. You can do this with iteration, you can iterate over all the keys checking each one to see if it's the one you want. Okay, but Python provides a better way. Just like with other sequenced types, you can use the in operator with dictionaries. Okay, so I can ask the question, is England in capitals? So here, England in capitals checks if England is a key in the capitals dictionary and evaluates to true if it is and false, otherwise. Now, just checking in the keys, so Lima is in the dictionary, right? But it's a value, right? So when I have Lima in capitals, that should evaluate to false because Lima is not a key. So I'd expect England in capitals to be true and Lima in capitals be false. Again, I can be more explicit by checking in the keys here with capitals.keys. I can check if Moscow is there, shouldn't be because it's not a key, and it's very obvious now that I'm checking in the keys. I can also check if Italy is in capitals.keys, which it should be. We know that it is. Now, finally, if I really want to check in the values instead, I should use capitals.values that gives me back a sequence of values, and I can check is Houston one of the capital cities, or is Beijing one of the capital cities? And, well, maybe Houston should be in there, but it's not in my dictionary, so let's check this out. Okay, so we get true. England is in fact a key in capitals and then false because Lima is not a key in capitals. And, again, we get false and true when we check if Moscow and Italy are in the keys explicitly, right? Which was expected. And then finally, when we want to check in the values, we look for the cities Houston and Beijing, and we find that Beijing is there, and Houston is not. Now, you've seen how we can iterate over dictionaries. There are several different ways of doing this. We can directly iterate over the dictionary. We can explicitly iterate over the keys of the dictionary, or we can explicitly iterate over the values. Also, usefully, we can iterate over the items so that we get the key value pairs directly. And, finally, if we don't actually need to iterate over the dictionary, rather we just want to check if something is in the dictionary, we can use the in operator, and we can use it directly on the dictionary again to look in the keys, or we can be explicit about whether we're looking in the keys or the values. You also learn here Houston, unfortunately, is not the capital of the United States.