Course
Remove Dictionary Items
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
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 numbersprint ("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 tupleSyntax
val = dict.popitem()
Return Value
The
popitem()
method return a tuple contain key and value of the removed item from the dictionaryExample
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: {}
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.