Let’s start with a first example:
'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.
’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.
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[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.
# 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 ().
# This line of code will give us the empty dictionary Dict.
We can also delete the key of the dictionary using the pop().
$ print(Dict.pop(1));
Ayan
Dictionary under dictionary
We can have a dictionary as a value associated with some key.
$ print(Dict)
If we will execute the above code then we will get the following output:
Let’s see how we can access the elements of the inner dictionary.
$ 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
Python
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.
$ 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
False
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.
from Linux Hint https://ift.tt/3dsKN4R

0 Comments