Guide to Using the get Function in Python Dictionaries to Configure Fallback Lookup Values
So far in this section on Python dictionaries you've seen how we can query elements in a python dictionary collection
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

The syntax for doing is by performing something like this where I say teams and then put in the name of the key and then that is going to perform the query.

teams['astros']

So, in this case, let's imagine that we're building some type of sports Web application and we want to have a featured team so I can say featured team store this in a variable. And now if I try to print out the featured team you'll see that it works and it prints out the players for the Astros

large

because we looked up the Astro's and there is a key rate here and that all works perfectly.

Now, what happens if we try to look up a key that does not exist in the dictionary. If I try running this now you'll see that we get an error and this gives us a full traceback and it says a key error Mets right here

large

which if you start as you're starting to learn what the error messages mean what this means is that there is no key named mets inside of this team's dictionary and this is fine sometimes there is going to be plenty of times where you are going to want to get this kind of an error because you do want an error to be thrown if the key doesn't exist. But there are also times where you may want to set up some type of default value.

So for an example like this. This is actually a perfect example of when you'd want that default value because you don't want an error coming up or the site breaking. Instead what you would want is to just have a fallback and that's what we can do with the get function inside of Python dictionaries. So what I'm going to do in order to use get is I'm just going to get rid of this entirely and I'm going to call on the team's dictionary and pass it the function get.

Now get takes two arguments. The first is a key we're looking for. So in the case of the Mets which does not exist inside of our team's dictionary and then also it takes whatever you want that backup to be. So if I want it to be simply a string that says no featured team then let's see what happens here. So if I run this we no longer get the error. Now it simply prints out no featured team.

large

Now, this is considered a best practice in the Python community to make sure that you are catching any kind of scenarios that have a situation like this where we're looking up a key that may or may not exist and you want to have some type of fallback and so this is going to give you instant feedback to let you know that what you tried to look up doesn't actually exist inside of the team's dictionary.

Now if we were to type in one that works. If I were to type in Yankees right here and run it you can see that it returns properly.

large

So this gives us almost like a conditional approach so I have performed these kinds of checks and other programming languages where I check to see something like this where I say if teams. And then you know whatever the name of the team is when that doesn't exist. So if Teams METS and then check to see does that throw an error or does that work or does it return with nothing. If so then I want you to perform these tasks. Else I want you to perform these tasks. And what we're doing here is exactly that and that's one very nice thing about this get function is it gives us the ability to have multiple processes and multiple checks all happen automatically and so that's why the function has become a go-to selector whenever you're working with lookups inside of Python dictionaries.

Code

teams = {
  "astros": ["Altuve", "Correa", "Bregman"],
  "angels": ["Trout", "Pujols"],
  "yankees": ["Judge", "Stanton"],
  "red sox": ['Price', 'Betts'],
}

featured_team = teams.get('yankees', 'No featured team')

print(featured_team)