Removing Dictionary Items in Python: Deleting Key-Value Pairs

thumb_up 1  ·  sell Python dictionary item removal, Deleting key-value pairs in Python dictionaries, Removing items from a Python dictionary

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: 
 {}





The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back