oursolutionarchitectoursolutionarchitect
  • Python Questions and Answers
  • Python - Programming Examples
  • Python - Quick Guide
  • Python - Useful Resources
  • Python - Discussion
    • Selected Reading
    • Q&A

    Python - Remove Dictionary Items


    Using del Keyword

    Python's del keyword deletes any object from the memory. Here we use it to delete a key-value pair in a dictionary.

    Syntax

    del dict['key']
    

    Example

    numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
    print ("numbers dictionary before delete operation: \n", numbers)
    del numbers[20]
    print ("numbers dictionary before delete operation: \n", numbers)
    

    It will produce the following output

    numbers dictionary before delete operation:
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty', 40: 'Forty'}
    numbers dictionary before delete operation:
     {10: 'Ten', 30: 'Thirty', 40: 'Forty'}
    

    Example

    The del keyword with the dict object itself removes it from memory.

    numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
    print ("numbers dictionary before delete operation: \n", numbers)
    del numbers
    print ("numbers dictionary before delete operation: \n", numbers)
    

    It will produce the following output

    numbers dictionary before delete operation:
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty', 40: 'Forty'}
    Traceback (most recent call last):
     File "C:\Users\mlath\examples\main.py", line 5, in <module>
      print ("numbers dictionary before delete operation: \n", numbers)
                                                               ^^^^^^^
    NameError: name 'numbers' is not defined
    

    Using pop() Method

    The pop() method of dict class causes an element with the specified key to be removed from the dictionary.

    Syntax

    val = dict.pop(key)
    

    Return value

    The pop() method returns the value of the specified key after removing the key-value pair.

    Example

    numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
    print ("numbers dictionary before pop operation: \n", numbers)
    val = numbers.pop(20)
    print ("nubvers dictionary after pop operation: \n", numbers)
    print ("Value popped: ", val)
    

    It will produce the following output

    numbers dictionary before pop operation: 
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty', 40: 'Forty'}
    nubvers dictionary after pop operation: 
     {10: 'Ten', 30: 'Thirty', 40: 'Forty'}
    Value popped:  Twenty
    

    Using popitem() Method

    The popitem() method in dict() class doesn't take any argument. It pops out the last inserted key-value pair, and returns the same as a tuple

    Syntax

    val = dict.popitem()
    

    Return Value

    The popitem() method return a tuple contain key and value of the removed item from the dictionary

    Example

    numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
    print ("numbers dictionary before pop operation: \n", numbers)
    val = numbers.popitem()
    print ("numbers dictionary after pop operation: \n", numbers)
    print ("Value popped: ", val)
    

    It will produce the following output

    numbers dictionary before pop operation: 
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty', 40: 'Forty'}
    numbers dictionary after pop operation: 
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty'}
    Value popped:  (40, 'Forty')
    

    Using clear() Method

    The clear() method in dict class removes all the elements from the dictionary object and returns an empty object.

    Syntax

    dict.clear()
    

    Example

    numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
    print ("numbers dictionary before clear method: \n", numbers)
    numbers.clear()
    print ("numbers dictionary after clear method: \n", numbers)
    

    It will produce the following output

    numbers dictionary before clear method: 
     {10: 'Ten', 20: 'Twenty', 30: 'Thirty', 40: 'Forty'}
    numbers dictionary after clear method: 
     {}