Dictionaries in Python are another kind of containers, but there are not so different from the container we have previously studied, SETS. In fact, you can consider them as an advanced variation of set.
WHAT IS A DICTIONARY?
A Dictionary in Python is used to assign a certain value to a specific key. In simple words, If we want to a print/use a value from a set of elements, we can use the 'key' assigned to that value to refer to that element.
In my last article, we talked about Sets in python (for those of you don't know, this is what I am talking about PYTHON BEGINNER'S GUIDE | SETS IN PYTHON). And if you remember one of the key factors of a set is that It does not support indexing, which means you cannot use/print a specific element from a set using it's index value, because it sorts the elements in random order and thus a specific index value cannot be assigned to an element. So, you can take Dictionaries as a way to tackle this problem in sets. Basically, a dictionary allows you to assign an "index value"(key) to an element.
Let's take a look at an example to understand how a dictionary actually works:
NOTE: The colon (:) assigns the value to the key
ERROR:
Let's suppose we call a value in the above example using the key '3'. This is what happens when we run the program:
An error pops up.....
This is because we never assigned a value to key '3', so far we have only assigned values to '1' and '2'.
SOLUTION:
Now obviously you want a solution to this problem, because you don't want an error to stop your program from running when you are working on big projects. Trust me when I say, there will be times when you are just writing code to prepare for the worst, and sometimes the worst is going to be, 'a key with no value assigned to it', and this should not stop your huge program from running.
THERE IS A SOLUTION TO THIS BUTT-HURTING PROBLEM. Though it is not a direct solution like, don't expect it to assign value to your otherwise empty key. It will only prevent the error from popping up which in result will keep your program running.
The solution is to use the get() function:
get() FUNCTION:
We can use the get() function to evade the above mentioned error in the following way:
If the key is empty as show in the above example, the get() function will return a 'None' value (more on this later).
So basically, if the key is empty, the get() function will not print any value but not give any error either.
ASSIGNING VALUE WHILE PRINTING:
The get() function has another feature with respect to keys and values. You can assign a value to a key while printing it. So this is an awesome way to assign a value to a key, IF the key is empty.
WELL....WHAT IF THE KEY IS NOT EMPTY AND HAS A VALUE ASSIGNED TO IT? Don't worry, if by any chance a value is already assigned to a key it will simply ignore assigning a new value and print the original/pre-assigned value.
CREATING DICTIONARIES USING LISTS:
That is right, you can create a dictionary using a list as well. BUT you cannot do it the same way I just told you using sets.
Creating dictionaries using lists is a little different.
Let's say you have two lists, 'keys' and 'values'
As the names suggest, the list 'keys' will store all the keys and the list 'values' will have all the values to be assigned to the respective keys.
Now to create a dictionary you will have to use a function dict(). This function will convert the list into a dictionary. REMEMBER, This function returns a list so make sure to use it in a variable so that the list returned can be store in that variable.
This is how you will create a dictionary:
Ok let's break it down
In line 5, the variable 'dic'( Please pronounce it correctly ) will store the dictionary which will be returned by the code that follows. The zip() function will merge the two lists together and return an object. The dict() function will simply convert that object into a dictionary.
And that is how you create a dictionary using lists.
ADDING A VALUE:
If we want to add a value to the above dictionary, you can do that in the following way:
And now the new dictionary is:
DELETING A KEY:
We use the del() function to delete a key.
Now the key 'self-taught' is deleted from the dictionary:
CONGRATULATIONS !!! You have learnt another VERY IMPORTANT TOPIC. Trust me, this knowledge will surely benefit you when you get advanced.
If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:
And do you know the best part? Joining it is FREE !!!
So go ahead click on the link and I will see you there.
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback