We can think of a dictionary in python the same as a normal translation dictionary where we see the meaning of any word. In simple language, we can think of a dictionary as a key-value pair as we look in the dictionary meaning of a word that goes with the dictionary used in python.  Dictionary is a data type in python which is very useful. Dictionary will have a key and a value associated with it. In this tutorial, I help understand setting default dictionary python values and you can try on any system which have python installed.  We can search for the key in a dictionary and we can fetch the value associated with the key.

Let’s start with a first example:

$ dict={‘India’:‘New Delhi’, ‘USA’:‘New York’, ‘UK’:‘London’,

‘South Africa’:‘Cape Town’}

By using above code, we have created a dictionary named dict. If we want to print the dictionary dict we have to just run this code-

Above line of code will print all the key values that are present in the dictionary dict. We can see in the above example where countries are keys of dictionary and capitals of countries as their values.

Now for fetching the value from the dictionary we have to provide the key as input.

$ print(dict[‘India’])

’New Delhi’

In the above example we can see how we can fetch the particular value from the dictionary.  What do you think will happen if we search the country which is not present in the dictionary dict?

If we execute the code then we will get a key error. We will get key errors when we are searching for the key which is not present in the dictionary.

Now we will see one very important use case of a dictionary. Let’s say if we have keys in different formats then integer let’s say string or character even a list can also be the key of a dictionary.

$ d={1: [1, 2, 3, 4], ‘Ayan’: ‘Sharma’}

In the above dictionary d we can see we have different keys with different data types and the same goes with the values. We have one value as a list and another value is a string, so now we can see how good the dictionary is when it comes to handling different data types.

Adding key-value to Dictionary

Now we will see how we can add a key value pair to the dictionary.

$ Dict={}       # Declaration of the empty dictionary.

$ Dict[0]=’Welcome’   # added one key 0 with value ’Welcome’

$ Dict[1]=’to’       # added one key 1 with value ’to’

$ Dict[2]=’Python’   # added one key 2 with value ’Python’

Updating value of a key in Dictionary

We can also update the values of keys present in the dictionary.

If we run the above line of code, then the value of key 0 in the dictionary will change from Welcome to Thanks. This is how we can update the values in the dictionary.

Deleting the key-value from the dictionary

We can simply use the command (del.dictionary(key) ) this command will delete all the key value pairs present in the dictionary with the provided key.

$ del Dict[0]

# This line of code will delete the key value pair with key 0


$ del Dict[1]

#This line of code will delete the key value pair with key 1

If we want to remove all the key values pairs present in the dictionary. We can use the command clear ().

$ Dict.clear()

# This line of code will give us the empty dictionary Dict.

We can also delete the key of the dictionary using the pop().

$ Dict = {1: ‘Ayan’, ‘name’: ‘is’, 3: ‘Good’}


$ print(Dict.pop(1));


Ayan

Dictionary under dictionary

We can have a dictionary as a value associated with some key.

$ Dict = {1: ‘Python’,  3:{‘A’ : ‘Welcome’, ‘B’ : ‘To’, ‘C’ : ‘Python’}}


$ print(Dict)

If we will execute the above code then we will get the following output:

{1: ‘Python’, 3: {‘A’: ‘Welcome’, ‘B’: ‘To’, ‘C’: ‘Python’}}

Let’s see how we can access the elements of the inner dictionary.

$ print(Dict[3][‘A’])


$ print(Dict[3][‘B’])


$ print(Dict[3][‘C’])

If we execute the above code then we will get the values present in the inner dictionary.

To check if any key is present in the dictionary or not. We can use the function has_key(), has_key() function is a boolean function which returns true if a key is present in the dictionary or else returns false.

$ Dict = {1: ‘Ayan’, ‘name’: ‘is’, 3: ‘Good’}


$ print(Dict.has_key(‘1’))

# Dictionary has key 1 so output is True

print(Dict.has_key(‘2’))

#​ Dict does not have key2 so output is false

Output of the above code is

Conclusion

We have to be very careful while using a dictionary in python because unlike other data structures it stores key-value pairs, the key cannot be repeated if the key will be duplicated then the previous key will be overridden. Values can be repeated for different keys. Dictionaries are very useful when we have to store a key and value associated with it. Like in our first example we have the counties as a key and capitals of the countries as a value. Dictionary is very different from other data structures; it should be used whenever we have the key-value pair.

About the author

Using Python Dictionary Keys Python

Rahul Raheja

I am Rahul Raheja, Highly passionate writer, who loves creating an imaginary world with his writings.Business Development Consultant, Strategist,Blogger, Traveller, Motivational Writer & Speaker