Dictionary in Python.
Dictionary in Python.
In this post we are going to learn about Dictionary in Python. Let's start..!
The dictionary is a datatype in Python . In dictionary we stores a values in key value pair ( key : value ) . The key in dictionary used to give unique identity to each value stored in dictionary. In dictionary you can stored duplicate values but you can't specify a duplicate key. Your key should be unique or different. Also your key should be numeric or string ( numbers , alphabet or word ) . If your key is alphabet or string it should be in double or single cote ( " " or ' ' ) . To define a dictionary
First you have to give any name to it and then give a equal to symbol ( = ) and use curly bracket ( { } ). Now you can store value inside dictionary. Here to store values inside dictionary first specify the key and put a colon ( : ) and give value to that key.
To separate value inside dictionary use comma ( , ) in between.
To learn more about Dictionary datatype read my post on Datatypes in Python
Or simply Click here This will open a post on Datatypes in Python.
Syntax : dict_name = { key1 : value1 , key2 : value2 , ..... , keyn : valuen }
for ex. Mydict = { 1 : "sid" , 2 : "Jack" , 3 : 100 }
Here I have created a dictionary named as Mydict . To store values I have specify the keys ( my key is numeric so don't need to specify it in double or single cote; But your key is alphabetic then you have to specify it in single or double cote ) 1, 2, 3 and their values "sid " , "Jack" , 100 respectively.
In Python we have so many function to work with dictionary. Some of them here,
1} clear() :
As a name this function clears the dictionary or simply removes all elements from dictionary. This function does not take any parameter.
Syntax : dict_name.clear()
for ex. We have a dictionary named as Mydict , and we want to removes all elements from this dictionary. Simply write Mydict.clear( ).
2} copy( ) :
As a name this function makes a same copy of the original dictionary. This copy of dictionary we can print on console ( screen ) or store it in a variable. Or we can do so many operations on this copy without changing original dictionary and this is a main use of copy( ) function. This function does not take any parameter.
Syntax : dict_name.copy( )
for ex. we want to make a copy of Mydict and print it on console. Simply write Mydict.copy( ) in print statement.
3} get( ) :
This function used to fetch a value in dictionary using a it's key. You have to give key as a parameter in this function. This function will return a value of key that you have given. Your parameter can be variable or actual value.
Syntax : dict_name.get( key )
for ex. we want to access value of key '2' of Mydict. Simply write Mydict.get(2).
You are using this function but if you give a key as a parameter that not exists in dictionary. Then you will get a output None . But you want print some message instead of None when your output is None. We can achieve this by giving a extra parameter.
for ex. We are fetching value of key '5' in Mydict but there is no any key '5' and this will give you a output None. So add extra parameter. Simply write Mydict.get( 5, "No suck key found.!" ). This will print No such key found.! instead of None .
4} update( ) :
This function is used to update or add new elements to the dictionary. To use this function have to give a new element as a parameter .( { key : value } ).
Syntax : dict_name.update( { key : value } )
for ex. We want to add new value "new" to Mydict having key as '4'. Simply write Mydict.update( { 4 : "new" } ).
5} keys( ) :
This function is use to fetch all keys in dictionary. This function does not take any parameter. This function will return a list of all keys in dictionary.
Syntax : dict_name.keys( )
for ex. We want to print all keys in a Mydict. Simply write Mydict.keys( ).
6} values( ) :
This function is use to fetch all values in dictionary. This function does not take any parameter. This function will return a list of all values in dictionary.
Syntax : dict_name.values( )
for ex. We want to print all values in a Mydict. Simply write Mydict.values( ).
This is some functions of dictionary. Create your dictionary and try this all function.
Now Where we use dictionary ? Let's take real life example ; In our phone if we want a phone number of someone then we search their number using their names or the name that we have used to save their number. It means we want a phone number but we are using their names. so here name is key and phone number is a value. I hope you have understand the use of dictionary.
If you have any doubts in this post then please ask me in comment section below. I will try to solve it.
If you like these post and you have learned something from this then share this post with your friends and family.
Keep learning always.....!!! 😇😇😇