Dictionary get() method Python

The Python dictionary get( ) method returns the value of item using the key.The method accept one or two arguments.If one argument is passed it should be the key of the item.If no argument is passed you get an error.

>>> d={23:56 , 'New':'Neew' , 'Tom':'Boy' , 'C++':'Rocks'}
>>> d.get( 23 )
56
>>> d.get( 'Tom' )
'Boy'
>>> d.get()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    d.get()
TypeError: get expected at least 1 arguments, got 0

If we pass a non-existent key the function does nothing.

>>> d={23:56 , 'New':'Neew' , 'Tom':'Boy' , 'C++':'Rocks'}
>>> d.get( '23' ) #returns nothing
>>> d.get( 'NEW' ) #return nothing
>>> d.get( 'C++' )
'Rocks'

You can also pass two arguments to get() method.If you pass a correct key as the first argument then you can pass second argument as anything and the second argument has no purpose.

The second argument is used only if you pass a non-existent key as the first argument.In this case since the key is non-existent the method will return the second argument as the output.So the second argument can be used as a way of identify that the key is non-existent.

>>> d={23:56 , 'New':'Neew' , 'Tom':'Boy' , 'C++':'Rocks'}
>>> d.get( 23 , 'TExt' )
>>> 56
>>> d.get( 'New' , 'Meow' )
'Neew'
>>> d.get( '23' , 'Non-existent key passed' )
'Non-existent key passed'
>>> d.get( 'Pyhton' , 'Key does not exist' )
'Key does not exist'

Difference between get() and using sub-script to obtain the value

If you have read my tutorial on Python data type I am sure you have realized that instead of using get() we can easily use the sub-script and the key as the index to obtain the value of the item.For example consider the code below.

>>> d1=dict( zip( "Momonosuke" , 'Wano kuni ') )
>>> d1
{'M': 'W', 'o': 'k', 'm': 'n', 'n': ' ', 's': 'u', 'u': 'n', 'k': 'i', 'e': ' '}
>>> d1['o']
'k'
>>> d1['s']
'u'
>>> d1['k']
'i'
>>> d.get( 'e' )
' '

But the problem with using the sub-script is that if by mistake we pass a non-existent key than we get an error;a KeyError message.Sometimes such error message may disrupt the flow of the program so it may not be suitable.However,if we use get() and even if we pass a non-existent key we are exempted from getting error message.On top of that if we want the program to provide us with some signal when a user passed a non-existent key we can do so by using the second argument.

 
>>> d1=dict( zip( "Momonosuke" , 'Wano kuni ') )
>>> d1['O']
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    d1['O']
KeyError: 'O'
>>> d1.get( 'O' ) #no error message
>>> d1.get( 'O' , 'The key passed is non-existent,please type different key')
'The key passed is non-existent,please type different key'

As you can see by using the get() method we can decode the error easily which in our case is by allowing the method to return the message ‘The key passed is non-existent,please type different key’.Now any user will type the correct key unambiguously.

There is another method known as setdefault() which is similar to get() method,to know their difference visit the link Dictionary setdefault() method.



Leave a Reply

Your email address will not be published. Required fields are marked *