Removing Keys from a Dictionary in Python

Posted by Samath
  374
January 11, 2017

 

This program shows how to remove key(s) from a dictionary using Python Programming Language. 

You can remove a key from a dictionary by using the del statement and you can clear all of the keys from a dictionary by using the clear() method.

dic = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}

del dic['k1']
print 'dic after removing k1 = ', dic

dic.clear()
print 'dic after clearing all keys = ', dic