VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-use-dict-get-with-multidimensional-dict/

⇱ How to use dict.get() with Multidimensional dict? - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to use dict.get() with Multidimensional dict? - Python

Last Updated : 23 Jul, 2025

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:


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:


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:


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: 

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.


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. 


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.

Comment
Article Tags:
Article Tags: