How to Remove Value-Key Pair from Dictionary
To remove a value-key pair from a Python Dictionary you can use dictionary pop() method.
Example
Python3
# initializing dictionary race_rank = { "Rahul" : 8 , "Aditya" : 1 , "Madhur" : 5 } # removing a value-key race_rank.pop( "Rahul" ) #printing new dictionary print (race_rank) |
Output
{'Aditya': 1, 'Madhur': 5}
Python Dictionary pop() Method
Python dictionary pop() method removes and returns the specified element from the dictionary.
Example:
Python3
# inializing dictionary student student = { "rahul" : 7 , "Aditya" : 1 , "Shubham" : 4 } # priting original dictionary print (student) # using dictionary pop suspended = student.pop( "rahul" ) # checking key of the element print ( "suspended student roll no. = " + str (suspended)) # printing list after performing pop() print ( "remaining student" + str (student)) |
Output
{'rahul': 7, 'Aditya': 1, 'Shubham': 4} suspended student roll no. = 7 remaining student{'Aditya': 1, 'Shubham': 4}
Contact Us