Wan to remove the given key from a dictionary in Python, then this answer is your you. In this answer, we have shared a python program which will remove the given key from a dictionary in Python.
1 2 3 4 5 6 7 8 9 10 11 | d = {'a':1,'b':2,'c':3,'d':4} print("Initial dictionary") print(d) key=raw_input("Enter the key to delete(a-d):") if key in d: del d[key] else: print("Key not found!") exit(0) print("Updated dictionary") print(d) |
Program Output
1 2 3 4 5 | Initial dictionary {'a': 1, 'c': 3, 'b': 2, 'd': 4} Enter the key to delete(a-d):c Updated dictionary {'a': 1, 'b': 2, 'd': 4} |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.