Javatpoint Logo
Javatpoint Logo

Get() Method in Python

Python's get() function can be used to access the objects of a dictionary, meaning the keys and their corresponding values.

You can use the get() method following the below syntax:

dictionary.get(key, default)

The parameters can be described as follows:

  • "key": The "key" parameter refers to the key in the dictionary whose corresponding value you want to access.
  • "default": The default argument is a parameter that is optional and indicates what is returned if the key cannot be found. The function returns None to the console if the key is not found in the dictionary and no "default" argument is supplied.

The get() method searches the entire dictionary for the provided key, and if it exists, its associated value in the dictionary is returned. The default value (or None if no default value is specified) is returned if the key cannot be found.

An example code snippet:

Output:

3
None
0

Explanation:

In the above example, we have a dictionary of "cars" that contains the count of various cars. Using get(), we retrieve the default values of 0 for the keys "ferrari", "mercedes", and "alpine". If the corresponding key exists in the dictionary while using the get() method, its corresponding value is returned to the console. If not, a default value passed as an argument in the function call is returned to the console.

If the key is not found in the dictionary, you can use the get() function to avoid "KeyError". It is a safe technique for obtaining values from dictionaries, especially when the key is unknown.

  • Handling a default value: If the key is not found in the dictionary, you can use the get() function to specify a default value to be returned. This step is mandatory as there will be situations where the key does not exist in the dictionary.

Output:

Unknown_Area
  • Efficient alternative to indexing: When compared to square bracket indexing ([]), the get() function is a more efficient approach to obtaining values from dictionaries. When indexing is used, if the key is not found, a "KeyError" exception is thrown. On the other hand, Get() allows you to handle missing keys graciously without throwing an exception.
  • Dealing with mutable default values: As the second argument to get(), you can specify a changeable default value. However, caution is advised because the same mutable object is used as the default value for all missing keys. If you change the mutable default value, subsequent retrievals when the key is missing will be affected. Here's an illustration:

Output:

[1]
[2]
[]

Explanation:

In this example, the default value [] is a changeable list. The value for the key 'list' is appended to the list when we retrieve it. When we later obtain the value for the key 'count', it adds 2 to the same list. Consequently, retrieving the value for the key 'count' again includes both 2 and 1 because it is the same list that was previously modified.

  • Safe handling of None values: The get() method is useful when working with dictionaries that may contain None as an acceptable value for some entries. Setting None as the default value while using square bracket indexing ([]) might be perplexing. The get() method is capable of distinguishing between the key with their values as None and the missing keys.

Output:

ERROR!
None
None
None
Traceback (most recent call last):
 File "", line 12, in 
KeyError: 'key3'

Explanation:

In this example, using square bracket indexing, providing None as a default value (result1 = data['key1']) returns the actual value None. When attempting to access a non-existent key (result2 = data['key3']), a KeyError is raised. On the other hand, providing None as the default value with get() method, returns the actual value None (result3 = data.get('key1', None)). Similarly, when attempting to retrieve a non-existent key (result4 = data.get('key3', None)), the default value None is returned.

  • Alternative to 'if' statement for default values: Generally, if the statement is used to check the existence of the key provided. The get() method can be used to avoid that. While using the get() function, it searches for the key; if found, its corresponding value is returned. Else, the default value is returned, which is provided during the function call. In most of cases, the default value is set to 0.

Output:

Unknown
Unknown

Explanation:

In this example, we can clearly observe that there is no need for a conditional statement to check for the key "city" by using the get() method.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA