get() method in python returns the value for the key if the key is in the dictionary. If the key is not present in a dictionary it returns None. It also takes another optional parameter which is the value that will be returned if the key is not found in a dictionary.
Syntax: dict.get(key, value)
Here, the key is a must parameter which is a key whose value we want to get from a dictionary and value is an optional field which is a value that is returned when a specified key is not found in a dictionary. The default value of value is None.
Example 1:
# Single-dimension dictionary
d = {'jhon': 22, 'sanie': 34, 'munk': 19}
# Print value for 'sanie' if present; otherwise, print 'Not found'
print(d.get('sanie', 'Not found'))
Output
34
Now, let's see how to use dict.get() with a multidimensional dictionary. For a multidimensional dictionary, we use .get() multiple times in a single statement.
Example 2:
# Multidimensional dictionary example using dict.get()
d = {
'India': {'captain': 'Virat', 'Batsman': 'Rohit', 'Bolwer': 'Bumrah'},
'England': {'captain': 'Root', 'Batsman': 'Buttler', 'Bolwer': 'anderson'},
'Australia': {'captain': 'Paine', 'Batsman': 'Warner', 'Bolwer': 'Starck'},
'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz', 'Bolwer': 'Aamir'},
'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle', 'Bolwer': 'Narayan'}
}
# Find the Batsman for India; return 'Not Found' if not present
print(d.get('India', {}).get('Batsman', 'Not Found'))
Output
Rohit
You can see it gives the correct output. Let's understand the working of this dict.get(). First dict.get() return all values of key 'India' which is a dictionary. it returns {'captain':'Virat','Batsman':'Rohit','Bolwer':'Bumrah'} now for this dictionary we again use get() method do find value. So for 'Batsman', it reruns 'Rohit'.
Example 3:
# Multidimensional dictionary example using dict.get()
d = {
'India': {'captain': 'Virat', 'Batsman': 'Rohit', 'Bolwer': 'Bumrah'},
'England': {'captain': 'Root', 'Batsman': 'Buttler', 'Bolwer': 'anderson'},
'Australia': {'captain': 'Paine', 'Batsman': 'Warner', 'Bolwer': 'Starck'},
'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz', 'Bolwer': 'Aamir'},
'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle', 'Bolwer': 'Narayan'}
}
# Find the 'Fielder' for India; return 'Not Found' if the key is missing
print(d.get('India', {}).get('Fielder', 'Not Found'))
Output
Not Found
You can see that fielder does not exist in a dictionary that is returned by 'India'. And that's why it gives 'Not Found' as output. But there is one problem using this get() method. If the value corresponding to the first key is not found then it will return a string and the second get() method will apply to the string. So this will give an error as dict.get() is a method for dictionary not for string.
Example 4:
# Example of dict.get() with multidimensional dict
dict = {'India': {'captain': 'Virat', 'Batsman': 'Rohit',
'Bolwer': 'Bumrah'},
'England': {'captain': 'Root', 'Batsman': 'Buttler',
'Bolwer': 'anderson'},
'Australia': {'captain': 'Paine', 'Batsman': 'Warner',
'Bolwer': 'Starck'},
'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz',
'Bolwer': 'Aamir'},
'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle',
'Bolwer': 'Narayan'}
}
# find batsman for new zealand
# return Not Found if not exists in dict
# if new zealand not found in dict will result in error
print(dict.get('new zealand').get('Batsman', 'Not Found'))
Output:
Traceback (most recent call last):
File "/home/4deb2392dee0ab15dec836bca68a69e2.py", line 12, in <module>
print(dict.get('new zealand').get('Batsman', 'Not Found'))
AttributeError: 'NoneType' object has no attribute 'get'
This gives 'NoneType' object error as string datatype has no method called get(). So for solving this error, we will use the second parameter of get() method which is for default output if the key is not found in a dictionary. We will return an empty dictionary if a key does not exist in the dictionary.
# Multidimensional dict example
dict = {
'India': {'captain': 'Virat', 'Batsman': 'Rohit', 'Bolwer': 'Bumrah'},
'England': {'captain': 'Root', 'Batsman': 'Buttler', 'Bolwer': 'anderson'},
'Australia': {'captain': 'Paine', 'Batsman': 'Warner', 'Bolwer': 'Starck'},
'Pakistan': {'captain': 'Babar', 'Batsman': 'Hafiz', 'Bolwer': 'Aamir'},
'West Indies': {'captain': 'Pollard', 'Batsman': 'Gayle', 'Bolwer': 'Narayan'}
}
# Find 'Batsman' for 'new zealand'; returns 'Not Found' if key doesn't exist
print(dict.get('new zealand', {}).get('Batsman', 'Not Found'))
Output
Not Found
In the output, you can see this works perfectly even if the first key is not found in a dictionary. We will also use the same approach for higher dimensions. Now let's see if we can use this for higher dimensions.
# Use of dict.get() in a multidimensional dictionary
data = {
'emp1': {'Name': {'First Name': 'Joe', 'Last Name': 'tribiani'}, 'age': 32},
'emp2': {'Name': {'First Name': 'Mark', 'Last Name': 'Adam'}, 'age': 20},
'emp3': {'Name': {'First Name': 'luci', 'Last Name': 'truk'}, 'age': 47},
}
# Retrieve 'Last Name' for emp2; returns 'Not Found' if any key is missing
print(data.get('emp2', {}).get('Name', {}).get('Last Name', 'Not Found'))
Output
Adam
This method works perfectly and can be applied to dictionaries of any depth. The key idea is to provide an empty dictionary ({}) as the default return value in each get() call—except for the final one. This ensures that if a key is missing, the subsequent get() still operates on a dictionary rather than returning None (which would cause an error when calling get() on it). For the final get(), you can specify any string (or other value) that you want to display if the key is not found.