Python Dictionary

Introduction

In this article, we will learn about one of the most important data type, which is dictionary. The dictionary contains the two values first ‘key’ and another is value, separated by colon (:) and a key-value pair is written in { }. Key is unique and a value can be repeated. Dictionary data type is easy to traverse and easy to find the value.

Syntax-

     Dictionary_name={‘key’:’value’,’key’:’value’}

Example-
  1. dict1={'name':'ajay','skill':'python'}  
Accessing value in the dictionary-

To access the value of the dictionary, we can use the key along with a square bracket.

Example-
  1. dict1={'name':'ajay','skill':'python'#list definition  
  2. print("name:",dict1['name']) #print name  
  3. print("skill:",dict1['skill']) #print skill  
Output-
 
Updating dictionary-

We can update an existing value, using the key and we can also add a new pair of the key and value.

Example-
  1. dict1={'name':'ajay','skill':'python'}  
  2. print("dictionary before change: ",dict1) #change exist item  
  3. dict1['skill']='c#' #change existing element  
  4. print("dictionary after change: ",dict1) #add new entry  
  5. dict1['exp']=2 #add new key and value pair  
  6. print("after add new element: ",dict1)  
Output-
 
Deleting Dictionary element-

We can delete an individual element or whole dictionary and empty dictionary.

Example-
  1. dict1={'name':'ajay','skill':'python'}  
  2. print("dictionary before delete: ",dict1) #delete individual element  
  3. del dict1['skill']  
  4. print("after delete individual element:",dict1) #clear dict1  
  5. dict1.clear()  
  6. print("after clear:",dict1)  #delete whole dict1  
  7. del dict1  
  8. print("after deleting whole dict1:",dict1)  
Output-
 
Built-in Dictionary functions are listed below-

len(dict)

Returns the total number of items, existing in the dictionary.

Syntax-

len(dict)

return value- number (length of dictionary)

Example-
  1. dict1={'name':'ajay','skill':'python'}  
  2. print("length of dict1 is:",len(dict1)) # calculating length  
Output-
 
str(dict)

It converts dictionary to string

Syntax-

str(dict)

return value- return string

Example-
  1. dict1={'name':'ajay','skill':'python'}  
  2. str1=str(dict1) #convert dict1 to string  
  3. print("string",str1)  
Output-
 
type()

Type function returns the variable data type.

Syntax-

type(dict)

return value- It returns the datatype of passed variable.

Example-
  1. dict1={'name':'ajay','skill':'python'}  
  2. print("datatype of dict is:",type(dict1))  
Output-
 
Other dictionary functions are-

dict.clear()

It removes all the elements of dictionary.

Syntax-

dict_name.clear()
 
return- no return

Example-
  1. dict1={'name':'ajay','ph':9990600495}  
  2. print("dict1 before execute dict1.clear: ",dict1)  
  3. dict1.clear()  
  4. print("dict1 after execute dict1.clear: ",dict1)  
Output-

 
dict.copy()

It copies dictionary and returns a new copy of dictionary.

Syntax-

dict2=dict1.copy()

return- return dictionary

Example-
  1. dict1={'name':'ajay','ph':9990600495#copy dict1 in dict2  
  2. dict2=dict1.copy()  
  3. print("dict1: ",dict1)  
  4. print("dict2: ",dict2)  
Output-
dict.fromkeys()

It creates a new dictionary with the keys and default value set to zero ‘0’.

Syntax-

dict_name=dict.fromkey(seq,values)

return- dictionary
 
Example-
  1. seq1=['name','age','sex']  
  2. dict1=dict.fromkeys(seq1)  
  3. print("dict1 :",dict1)  
Output-
dict.key(key)

It returns the value of the given key and the key is not passed. It returns none.

Syntax-
Variable_name=dict_name.get(key)

Return- It returns the value of the given key.

Example-
  1. dict1={'name':'ajay malik','sex':'male'}  
  2. print("value at name key are:",dict1.get('name'))  
Output-
 
dict.keys()

This method returns the keys of dictionary.

Syntax-

Dict_name.keys()

Return- Dictionary key.

Example-
  1. dict1={'name':'Ajay','Age':23}  
  2. print("keys in dict1: ",dict1.keys())  
Output-
 
dict1.values()

It returns the list of all the values of dictionary.

Syntax-

dict.values()

Return- return values

Example-
  1. dict1={'name':'Ajay','Age':23}  
  2. print("values in dict1: ",dict1.values())  
Output-
 
Summary

In this article, you learnt how to create dictionary, adding key value pair into dictionary and you also friendly about predefine function of dictionary.