Skip to content
Related Articles

Related Articles

How to use dict.get() with multidimensional dict?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 05 Nov, 2021

The 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:

Python3




# single dimension dictionary
d = {'jhon': 22, 'sanie': 34, 'munk': 19}
  
# return value if found if not then return Not found
print(d.get('sanie', 'Not found'))


Output:

34

Example 2:

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. 

Python3




# 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 india
# return Not Found if not exists in dict
print(dict.get('India').get('Batsman', 'Not Found'))


Output:

Rohit

Example 3:

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

Python3




# 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 fielder for india
# return Not Found if not exists in dict
print(dict.get('India').get('Fielder', 'Not Found'))


Output:

Not Found

Example 4:

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. 

Python3




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

Python3




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

Python3




# use of dict.get() in multidimensional dictionary
  
dict = {'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},
        }
  
# return Not Found if emp2 is not found
# or Name is not Found or Last name is not found
print(dict.get('emp2', {}).get('Name', {}).get('Last Name',
                                               'Not Found'))


Output:

Adam

So this works perfectly. we can use this for any dimension. Just add dictionary as default return value if the key is not found in a dictionary. Add dictionary as default return value except for the last get() method else it will output an empty dictionary. Add any string in the last get() method that you want to display if the value is not found.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!