Skip to Content

Get Value for a Key in a Python Dictionary

In this tutorial, we will look at how to get the value corresponding to a key in a Python dictionary with the help of some examples.

value for a key in python dictionary

For a Python dictionary, you can get the value of a key using the [] notation. Alternatively, you can also use the dictionary get() function. The following is the syntax:

# value corresponding to a key in a dicitonary
sample_dictionary[sample_key]
# using dictionary get() function
sample_dictionary.get(sample_key)

Both the methods give the value corresponding to the key in the dictionary. If the key is not present in the dictionary, the [] notation method will raise a KeyError whereas the dictionary get() function will return None.

Let’s look at some examples.

Let’s say we have a dictionary mapping employee names to their respective departments in an office. If you want to know the department for a particular employee, for example, “Dwight”, you’ll have to access its corresponding value in the dictionary.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# value corresponding to the key "Dwight"
print(employees["Dwight"])

Output:

Sales

Here we access the value for the key “Dwight” in the dictionary employees using the [] notation. We find that the employee is in the “Sales” department.

Let’s look at another example. What happens if we access a key that is not present in the dictionary using this method?

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# value corresponding to the key "Dwight"
print(employees["Ryan"])

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [2], in <module>
      2 employees = {
      3     "Jim": "Sales",
      4     "Dwight": "Sales",
      5     "Angela": "Accounting"
      6 }
      7 # value corresponding to the key "Dwight"
----> 8 print(employees["Ryan"])

KeyError: 'Ryan'

We get a KeyError because we’re trying to access a key that does not exist in the dictionary.

The Python dictionary get() function returns the value corresponding to a key in the dictionary. If the key is not present, it returns None.

Let’s take the same example as above. What’s the department for “Dwight”?

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# value corresponding to the key "Dwight"
print(employees.get("Dwight"))

Output:

Sales

We get “Sales” as the output, the same as we got in the above example.

Now, this method does not give any errors if the key is not present in the dictionary. Let’s see what we get if we try to access value for a key that doesn’t exist in the dictionary.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# value corresponding to the key "Dwight"
print(employees.get("Ryan"))

Output:

None

We get None as the output.

In this tutorial, we looked at two methods to get the value corresponding to a key in a Python dictionary.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.