Algorithm


1. Input the dictionary and the key to be deleted.
2. Check if the key exists in the dictionary.
3. If the key is present:
    a. Delete the key-value pair from the dictionary using the `del` statement.
    b. Print a message indicating that the key has been deleted.
4. If the key is not present:
    a. Print a message indicating that the key does not exist in the dictionary.
5. Output the modified dictionary.

Code Examples

#1 Code Example- Python Programing Using del keyword

Code - Python Programming

my_dict = {31: 'a', 21: 'b', 14: 'c'}

del my_dict[31]

print(my_dict)
Copy The Code & Try With Live Editor

Output

x
+
cmd
{21: 'b', 14: 'c'}

#2 Code Example- Python Programing Using pop()

Code - Python Programming

my_dict = {31: 'a', 21: 'b', 14: 'c'}

print(my_dict.pop(31))

print(my_dict)
Copy The Code & Try With Live Editor

Output

x
+
cmd
a
{21: 'b', 14: 'c'}
Advertisements

Demonstration


Python Programing Example to Delete an Element From a Dictionary-DevsEnv