Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

2.3 Dictionary Comprehensions

Similar to its sibling the list comprehension, a dictionary comprehension is nothing more than a shorthand way of creating a new dictionary collection. While it may seem a little more complicated than the list version, it is actually quite simple to understand. I'll show you how in this lesson.

2.3 Dictionary Comprehensions

Similarly to what we did with the list in the previous lesson by translating our for loop that we were using to create a list into a list comprehension to be able to do that a little bit in more of a shorthand notation, we can do the same thing with a dictionary, so let's see how we would do that. So here's our example, once again we have our character count which defines a list of key value pairs, where the key is the letter that was found let's say within a sentence and the value is the how many times that we found that character within the dictionary or within a sense. And then, we wanted to create a character frequency dictionary that was going to merge together all of these duplicate letters that were just upper and lower case. So what we did was we loop through the key value pairs. We check to see if the key existed already within the dictionary. If it did, then we just added the new value to the existing value. And if that key did not exist, then we inserted the new key and assigned a value to it. Now, once again, this absolutely works. But, if you think about it, this is a very simplistic concept of what we're trying to do. And, including spaces here, this little program took ten lines of code to simply do those operations. And, I guess, if you took the spaces out and condensed it a little bit, you could get away with eight maybe, but that still seems like a lot to do such a simple operation. I really wish there was an easier way to do it or at least a more condensed way to do it. Well, it just so happens that there is. So, let's take a look at what a dictionary comprehension looks like. So let's walk through the blueprint. We're gonna create a variable name so this is very similar to what we did before. What we want that resulting destination dictionary to be called, and because it's a dictionary we're gonna use open and close curly brackets. So, like we did with the list comprehension, we then put the logic of what we want to go into it within the curly brackets. So we're going to drop down a line here just so it's a little bit easier to read. And what it's going to ultimately be is we're going to define whatever we want our key to be. So it can be some sort of expression or just the definition of what letter or whatever we want that key to be, we're gonna define that here and then we're going to use a colon and then we're gonna define whatever that value is. It could be some sort of expression, we could put some logic in there, some conditionals, whatever we wanna do. And then we are, ultimately, once again going to have a, for loop, at the end here, so it's gonna be, for some loop variable that's going to be in some collection or in this case it's gonna be within probably a dictionary, right? But it doesn't have to be. It could be something else if we wanted it to. That is basically it. So we can condense a whole lot of logic down into something very simple like this dictionary comprehension. So let's see how we can translate what we've already done up here into a dictionary comprehension. So we're gonna say our character frequency, we'll keep that same name, is gonna be equal to, open and close curly brackets. And now within here, we wanna define what our key is going to be. Now, that the key that we did before anyway was gonna be the lowercase version of whatever it is we found within character count. So we're simply gonna keep that. We're gonna say, key.lower and that's going to be our key. Then we'll do our colon and now we need to define our values. So what is that gonna be? Well, there's an interesting way that we can handle doing this using a special function within Python. So let's go ahead and say we want it to be char_count.get. Now, this get function is going to take two arguments. It's gonna say, what is the key that I wanna get? And I wanna look for key.lower. If I find it, it's going to return back the value associated with it. If it doesn't find it, I can specify a default value and in this case we'll just say 0. If you do find it, give me back the value. If you don't find it, just give me back 0. But it's not enough to just say we wanna look for lower because in this case we wanna combine lowercase a with uppercase A, so then we'll simply say + char_count. Once again, we wanna get, but in this case, we wanna get the upper, so we wanna see if the upper of that key exists within our dictionary. If it does, give me back the value and add it back to the result here, if it doesn't, then we wanna add the default value of zero to whatever value we got back from this first operation. So our key is gonna be the lowercase version of whatever key we find in here, and the value is gonna be the combination of the lower and upper case versions that are found within that first dictionary of character count. And if either one of those don't exist we're just going to make default those values to zero. Then, as I said before we simply need to loop through this so we can say for key in char_count.keys. Just like that. So we'll go ahead and save that we'll pop down. We'll do another print here, print char_frequency and we'll go ahead and save that. So let's go ahead and give this guy a run. So I wanna run my dictionary version, and I have mis-defined a couple things here. So let's go back and take a look, and yes, what I did here is I'm talking about K here or key here. I'm talking about key here but over on the set of talking about K. So, we need to combine all these things and make sure that all the same. So just to make it simple we'll just say that these are all gonna be key like that and we'll save it. And I believe that should take care of it. it's come back. We'll go ahead run it again and there you go, we got to the same resulting point that we did before where we got all the combined letters that were found uppercase and lowercase. And if you take a look at this, that's pretty simplistic. We took all of this code and giving the benefit of the doubt the eight lines of code that were here excluding spaces and we got it down into four or five. So that's pretty cool. I tell you any time that I find the opportunity to condense the code. While still keeping it readable to other people that are going to have to maintain the code that I'm working on I definitely find that as a huge benefit to not only me but also to the rest of my team.

Back to the top