Python Dictionary Get – With Examples

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.

As stated above, get() is a dictionary function in python used to fetch the value of a specified key in the dictionary.

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:

  • 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.

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.

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.