Dictionary Methods in Python programming | Hands on coding

Python contains the following dictionary methods.
  • clear() function is used to clear or empty the dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary before clear:\n",student)
#this line will empty the dictionary
student.clear()
print("Dictionary after clear:\n",student)
"""
***Output***
Dictionary before clear:
 {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary after clear:
 {}
 """
 
  • This method returns a shallow copy of the dictionary.

dict1={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary 1:",dict1)
#copy dictionary1 into dictionary2
dict2=dict1.copy()
print("Dictionary 2:",dict2)
"""
***Output***
Dictionary 1: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary 2: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
 """
  • This method creates a new dictionary with specified key and value.

keyset=('key1','key2','key3')
dictionary1=dict.fromkeys(keyset)
print("Without Value",dictionary1)
dictionary2=dict.fromkeys(keyset,50)
print("With Value",dictionary2)
  • It returns the value of the specified key.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6
}
print("Student Name:",student.get("name"))
"""
***Output***
Student Name: Amisha
"""
  • It retirns all the key-value pair of the dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student.items())

"""
***Output***
Dictionary: dict_items([('name', 'Amisha'), ('rollno', 305), ('percent', 85.6)])
 """
 
  • It returns all the keys of the dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student.keys())

"""
***Output***
Dictionary: dict_keys(['name', 'rollno', 'percent'])
 """
 
  • This method is used to set a default value to a key.
  • If the specified key is present in the dictionary then it retunrs its value.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
#rollno is present in dictionary
#so it will return 305
x=student.setdefault("rollno")
print("Dictionary:",student)
print("Rollno:",x)
"""
***Output***
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Rollno: 305
 """
  • If the specified key is not present in the dictionary then it insert key into dictionary with default value 'None'.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student)
student.setdefault("Branch")
print("Dictionary:",student)
"""
***Output***
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6, 'Branch': None}
 """
 
  • We can also set value of key using setdefault function.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student)
#setting key with value
student.setdefault("Branch","Computer Science")
print("Dictionary:",student)
"""
***Output***
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Dictionary: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6, 'Branch': 'Computer Science'}
 """
  
  • It returns all the values of the dictionary.

student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Dictionary:",student.values())

"""
***Output***
Dictionary: dict_values(['Amisha', 305, 85.6])
 """
  
  • This method updates the dictionary with the key and value pairs.

#creating dictionary
student={
    "name":"Amisha",
    "rollno":305,
    "percent":85.6,
}
print("Student Info:",student)
#updating student info
student.update({"Branch":"Computer Science"})
print("Updated Student Info:",student)
"""
***Output***
Student Info: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6}
Updated Student Info: {'name': 'Amisha', 'rollno': 305, 'percent': 85.6, 'Branch': 'Computer Science'}
 """