

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Dictionary Elements in Python
You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.
To explicitly remove an entire dictionary, just use the del statement.
Example
Following is a simple example −
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} del dict['Name']; # remove entry with key 'Name' dict.clear(); # remove all entries in dict del dict ; # delete entire dictionary print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']
Output
This produces the following result. Note that an exception is raised because after del dict dictionary does not exist any more −
dict['Age']: Traceback (most recent call last): File "test.py", line 8, in <module> print "dict['Age']: ", dict['Age']; TypeError: 'type' object is unsubscriptable
Note − del() method is discussed in subsequent section.
- Related Questions & Answers
- Delete List Elements in Python
- Delete Tuple Elements in Python
- Delete items from dictionary while iterating in Python
- Delete elements in range in Python
- Delete elements with frequency atmost K in Python
- How to count elements in a nested Python dictionary?
- Delete elements in C++ STL list
- How to remove all the elements of a dictionary in Python?
- Convert string dictionary to dictionary in Python
- What is the basic syntax to access Python Dictionary Elements?
- Add elements to a Dictionary in Javascript
- Find common elements in three sorted arrays by dictionary intersection in Python
- Dictionary Methods in Python
- Updating Dictionary in Python
- Python - Dictionary has_key()
Advertisements