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}

Similar Reads

Definition and Use of Python Dictionary pop() Method

...

Syntax of Dictionary pop()

Dictionary pop() function in Python is an in-built function that is used to remove and return an element from a dictionary. It can take one or two arguments....

How to Remove Value-Key Pair from Dictionary

dict.pop(key, def)...

More Example of Dictionary pop() method

To remove a value-key pair from a Python Dictionary you can use dictionary pop() method....

Contact Us