Write a Python program to Rename key of a dictionary


This program defines a dictionary called "student" that contains 4 key-value pairs of information about a student named "Tara".

The first print statement outputs the original dictionary:

Then, the program renames the keys "Mark" and "RollNo". The student.pop("Mark") method is used to remove the "Mark" key from the dictionary and store its value (458) in a new variable. The same process is then repeated for the "RollNo" key.

Next, the program adds two new key-value pairs to the dictionary. The first uses the new variable containing the value of "Mark" and assigns it to a new key "Mark10". The second key-value pair adds a new key "RegNo" with the value of the old "RollNo" key.

Source Code

student = {
	"Name": "Tara",
	"RollNo":130046, 
	"Mark": 458, 
	"Age":16,
}
print("Before Rename Key of a Dictionary :",student)
student["Mark10"] = student.pop("Mark")
student["RegNo"] = student.pop("RollNo")
print("After Rename Key of a Dictionary :",student)

Output


Before Rename Key of a Dictionary : {'Name': 'Tara', 'RollNo': 130046, 'Mark': 458, 'Age': 16}
After Rename Key of a Dictionary : {'Name': 'Tara', 'Age': 16, 'Mark10': 458, 'RegNo': 130046}