[Complete Program] How to Set Default Dictionary Python Value?

[Complete Program] How to Set Default Dictionary Python Value?

About Python Dictionary:

  • Python Dictionary is a special data type in python which contains the data in the form of name-value pair.
  • Every name gets mapped to the value.
  • The value of the dictionary can be any valid data type of python including numeric data type.

In this post, we check, how to set default dictionary python value.

Why do you need to set a default value for a dictionary in Python?

Let’s make the thing simple. Take the instance. It will be easy to understand.

Suppose we are maintaining the capitals of BRICS association countries. The mapping of countries involved in BRICS association is mapped with their capitals.

Problem statement:

Write a Python program to find the capital of the BRICS association country. If it is not listed in the Python dictionary, it should return as ‘This Country is not listed in BRICS.’

dictBRICMap is dictionarywhich stores the BRICs countries as name and its capital as value.

>>>> dictBRICMap = {'Brazil':'Brasília', 'Russia':'Moscow', 'India':'New Delhi', 
'China':'Beijing', 'South Africa':'Cape Town'}

There are two typical ways of getting dictionary value from key:

The easiest way to find the capital of the country enrolled in BRICs is as follows.

>>>> print(dictBRICMap['India'])
'New Delhi'

We can also find it by calling dictionary function dict.get(‘key’) as follows.

>>>> print dictBRICMap.get('India')
'New Delhi'

What if we provide a key which is not present in the dictionary?

It throws an error as follows.

>>>> print dictBRICMap.get('Japan')
Traceback (most recent call last):
File "", line 1, in 
KeyError: 'japan'

It does not look good.

What if we display the string as “This country is not listed.” instead of throwing an error?

For this, we need to set the default value for the dictionary.

Simple Code to Set Default Dictionary Python Value

To tackle this, we can provide the default value for the key using get() method.

If the country does not match with any of the keys in the dictionary, it takes default dictionary python value as follow.

>>>> print(dictBRICMap.get('Japan','This country is not listed in BRICs.'))
'This Country is not listed in BRICs.'

For the end-user perspective, this text looks more genuine.

Many times it is not possible to provide all the dictionary values. Taking default dictionary Python value is good provision to tackle with invalid dictionary keys.

You can also prompt as ‘Key not present in the dictionary’ in general case if the user provides ‘invalid key’.

6 Comments

  1. Quick question: instead of: print(dictBRICMap.get('Japan','This country is not listed in BRICs.'))
    is there a way to reply '"Japan" is not listed in BRICS'

    Thanks.

    1. You can not do it directly. But you can make it happen indirectly.

      dictBRICMap = {'Brazil':'Brasília', 'Russia':'Moscow', 'India':'New Delhi', 
      'China':'Beijing', 'South Africa':'Cape Town'}
      
      search = 'Japan'
      out = dictBRICMap.get('Japan','No')
      
      if out=="No":
          print(f"{search} is  is not listed in BRICS")
      
      

Leave a Reply

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