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.
How to get the value of a key in a 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.
Using []
to access the key’s value
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?
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# 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.
Using dictionary’s get()
function
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 –
- Count Occurrences of a Value in a Python Dictionary
- Check If a Python Dictionary Contains a Specific Key
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.