Python Program to Delete an Element From a Dictionary – 2024

Hey guys, in this blog we will see a Python Program to Delete an Element From a Dictionary.

Table of Contents

Example 1: Using the del keyword

# Python Program to Delete an Element From a Dictionary

my_dict = {1: 'machine', 2: 'learning', 3: 'projects'}

del my_dict[3]

print(my_dict)

Output

{1: 'machine', 2: 'learning'}
  • Here we have used del keyword to delete a key-value pair from our dictionary.
  • We can do similar for a list also.

Example 2: Using pop()

# Delete an Element From a Dictionary

my_dict = {1: 'machine', 2: 'learning', 3: 'projects'}

print(my_dict.pop(3))

print(my_dict)

Output

projects
{1: 'machine', 2: 'learning'}
  • We can also use pop() to delete a key-value pair from a dictionary.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

Your email address will not be published. Required fields are marked *