Python Dictionary get() Method
Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).
Python Dictionary get() Method Syntax:
Syntax : Dict.get(key, Value)
Parameters:
- key: The key name of the item you want to return the value from
- Value: (Optional) Value to be returned if the key is not found. The default value is None.
Returns: Returns the value of the item with the specified key or the default value.
Python Dictionary get() Method Example
d = {'coding': 'good', 'thinking': 'better'}
print(d.get('coding'))
Output:
good
Example 1: Python get() Method with Second Parameter
Here, In the code 4 is not in keys, so the second parameter will print “Not found”.
d = {1: '001', 2: '010', 3: '011'}
print(d.get(4, "Not found"))
Output
Not found
Example 2: Python Dictionary get() method chained
The get() to check and assign in absence of value to achieve this particular task. Just returns an empty Python dict() if any key is not present.
test_dict = {'Gfg' : {'is' : 'best'}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using nested get()
# Safe access nested dictionary key
res = test_dict.get('Gfg', {}).get('is')
# printing result
print("The nested safely accessed value is : " + str(res))
Output:
The original dictionary is : {'Gfg': {'is': 'best'}}
The nested safely accessed value is : best
Time complexity: O(1) because it uses the get() method of dictionaries which has a constant time complexity for average and worst cases.
Auxiliary space: O(1) because it uses a constant amount of additional memory to store the dictionary and the string values.
Python Dictionary get() Method – FAQs
What does get()
do in Python dictionary?
The
get()
method in a Python dictionary returns the value for a specified key if the key is in the dictionary. If the key is not found, it returns a specified default value, which isNone
if no default value is provided.my_dict = {'name': 'Alice', 'age': 25}
# Get value for existing key
print(my_dict.get('name')) # Output: Alice
# Get value for non-existing key with default value
print(my_dict.get('gender', 'Unknown')) # Output: Unknown
# Get value for non-existing key without default value
print(my_dict.get('gender')) # Output: None
What is get()
method in list Python?
Python lists do not have a
get()
method. Theget()
method is specific to dictionaries. To access elements in a list, you use indexing.my_list = [1, 2, 3]
# Accessing list elements using index
print(my_list[0]) # Output: 1
print(my_list[1]) # Output: 2
print(my_list[2]) # Output: 3
What is find()
method in Python?
The
find()
method in Python is a string method that returns the lowest index of the substring if it is found in a string. If the substring is not found, it returns-1
.my_string = "Hello, world!"
# Find the index of a substring
print(my_string.find('world')) # Output: 7
# Find the index of a substring that doesn't exist
print(my_string.find('Python')) # Output: -1
What is the get value method in Python dictionary?
The
get
value method in a Python dictionary isget()
. It retrieves the value associated with a given key, with an optional default value if the key is not found.my_dict = {'name': 'Alice', 'age': 25}
# Using get method to retrieve value
print(my_dict.get('name')) # Output: Alice
print(my_dict.get('gender', 'Unknown')) # Output: Unknown
What is the default value of dictionary get()
in Python?
The default value of the dictionary
get()
method in Python isNone
if no default value is provided.my_dict = {'name': 'Alice', 'age': 25}
# Get value for a key with default value not specified
print(my_dict.get('gender')) # Output: None
# Get value for a key with default value specified
print(my_dict.get('gender', 'Unknown')) # Output: UnknownThese methods provide flexible ways to handle and retrieve data from Python dictionaries and strings