The python dictionary get method is used to return the value corresponding to a key in a dictionary. In this tutorial, we’ll look at the get()
method, its syntax, and use-cases along with some examples.
Before we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. For more, check out our guide on dictionaries and other data structures in python.
The get() method
As stated above, get()
is a dictionary function in python used to fetch the value of a specified key in the dictionary.
Syntax
The following is the syntax of the get()
function:
sample_dict.get(key, default)
Here, sample_dict is the python dictionary from which you want to get the value for the corresponding key.
Parameters:
- key: The key name whose value you want to get.
- default (optional): The value to be returned if the specified key is not present in the dictionary. It defaults to
None
.
Returns:
The get()
method returns:
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.
- The value corresponding to the key if the key is present in the dictionary.
- If the key is not present and the default is not specified, it returns
None
. - If the key is not present and the default is specified, then it returns the specified default.
Examples
Example 1: If the key is present
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# use get() to get the value corresponding to 'GOOG'
val = shares.get('GOOG')
# print the value
print("The number of shares of GOOG:", val)
Output:
The number of shares of GOOG: 50
In the above example, the dictionary shares
stores the number of shares of different companies in a sample portfolio. We use the python dictionary get()
method to fetch the number of shares of 'GOOG'
in the portfolio which returns 50
.
Example 2: If the key is not present and the default is not specified
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# use get() to get the value corresponding to 'TSLA'
val = shares.get('TSLA')
# print the value
print("The number of shares of TSLA:", val)
Output:
The number of shares of TSLA: None
In the above example, we use the get()
function to fetch the value corresponding to the key 'TSLA'
which does not exist in the dictionary shares
. And since we’ve not specified the default value, we get None
returned from the function.
Example 3: If the key is not present and the default is specified
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# use get() to get the value corresponding to 'TSLA'
val = shares.get('TSLA', 'Not present')
# print the value
print("The number of shares of TSLA:", val)
Output:
The number of shares of TSLA: Not present
In the above example, we use the get()
function to fetch the value corresponding to the key 'TSLA'
which does not exist in the dictionary shares. We provide a default value to be used when the key is not present as Not present
. Here, the function returns Not present
since the key was not found.
Alternative to the get() method
We can also use the subscript notation using square brackets []
to fetch the value corresponding to a key. This is quite a simple way of getting the value for a specific key in the dictionary.
Example: Using the subscript notation to get the value of a key
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# use get() to get the value corresponding to 'GOOG'
val = shares['GOOG']
# print the value
print("The number of shares of GOOG:", val)
Output:
The number of shares of GOOG: 50
In the above example, we get the value for the key 'GOOG'
in the dictionary using the expression shares['GOOG']
which returns 50
.
Unlike the get()
method which does not give any error even if the key is not present, using the subscript method gives a KeyError
if the key is not found in the dictionary.
Example: Using the subscript notation to get the value of a key not present in the dictionary
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# use get() to get the value corresponding to 'TSLA'
val = shares['TSLA']
# print the value
print("The number of shares of TSLA:", val)
Output:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-9-a28398371d1f> in <module>
3
4 # use get() to get the value corresponding to 'TSLA'
----> 5 val = shares['TSLA']
6 # print the value
7 print("The number of shares of TSLA:", val)
KeyError: 'TSLA'
In the above example, the expression shares['TSLA']
results in a KeyError
since the key 'TSLA'
is not present in the dictionary shares
.
For more on the get()
and other dictionary functions, refer to the python docs.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.