Dictionaries have a get() method

Okay here we are with yet another post. However this post is going to be short as in this post i am going to teach you a small, simple and very useful trick. Have you ever tried to access a value from a dictionary and got an exception that there is no value mapped to that key? Like this:

>>> dict = {'key1':2,'key2':3}
>>> dict['key1']
>>> dict['key3']

Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'key3'

Sometimes you do not want anything like this because this error can break your whole program and make it useless until you fix this issue. Is there any way through which we can see if a key is present in a dictionary or not and if it is not present then get a default value in it’s place? the chances are that you don’t know of any such method. Okay so here’s the trick. Just examine the code below and i will explain it later.

>>> dict = {'key1':2,'key2':3}
>>> dict['key1']
>>> a = dict.get('key3', 0)
>>> print a

So what is happening above? We are still accessing the same key but where has the exception gone? Let me introduce you to get() method which is available with a dictionary. What this method does is that it allows you to specify a default value which is returned in case the key is not present in the dictionary.

In our case there is no key3 in our dict but when we are using the get() method we are specifying the default value which should be returned in case if key3 is not present and the default value which i have set is 0. The syntax of the get method is:

dict.get('anykey','the default value to return if the key is not present')

However if we do not set a default value then this returns None if the key is not present. I hope it’s enough for you guys however if you want to further explore this get() method then google is your friend.

Do share your views in the comments below.



If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Be the first to leave a comment! 🎉

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊