Python Program to Delete a Key from a Dictionary

Introduction

Deleting a key from a dictionary is a common operation in Python programming, especially when you need to remove unwanted or unnecessary data. This tutorial will guide you through creating a Python program that deletes a specified key from an existing dictionary.

Example:

  • Input Dictionary: {'name': 'John', 'age': 25, 'city': 'New York'}
  • Key to Delete: 'age'
  • Output Dictionary: {'name': 'John', 'city': 'New York'}

Problem Statement

Create a Python program that:

  • Takes an existing dictionary.
  • Deletes a specified key from the dictionary.
  • Displays the updated dictionary.

Solution Steps

  1. Create a Dictionary: Initialize a dictionary with some key-value pairs.
  2. Delete a Key from the Dictionary: Use the del statement or the pop() method to delete a key from the dictionary.
  3. Display the Updated Dictionary: Use the print() function to display the updated dictionary.

Python Program

# Python Program to Delete a Key from a Dictionary
# Author: https://www.rameshfadatare.com/

# Step 1: Create a dictionary with key-value pairs
my_dict = {
    'name': 'John',
    'age': 25,
    'city': 'New York'
}

# Step 2: Take the key to delete as input from the user
key_to_delete = input("Enter the key to delete: ")

# Step 3: Delete the key from the dictionary if it exists
if key_to_delete in my_dict:
    del my_dict[key_to_delete]
    print(f"'{key_to_delete}' has been deleted.")
else:
    print(f"'{key_to_delete}' not found in the dictionary.")

# Step 4: Display the updated dictionary
print("The updated dictionary is:", my_dict)

Explanation

Step 1: Create a Dictionary with Key-Value Pairs

  • A dictionary my_dict is created with some initial key-value pairs, such as 'name': 'John', 'age': 25, and 'city': 'New York'.

Step 2: Take the Key to Delete as Input from the User

  • The input() function prompts the user to enter the key they want to delete from the dictionary. This input is stored in the variable key_to_delete.

Step 3: Delete the Key from the Dictionary if It Exists

  • The program checks if the key exists in the dictionary using the in keyword. If the key exists, the del statement is used to delete the key-value pair from the dictionary. If the key is not found, a message is displayed indicating that the key does not exist.

Step 4: Display the Updated Dictionary

  • The print() function is used to display the dictionary after the specified key has been deleted.

Output Example

Example 1:

Enter the key to delete: age
'age' has been deleted.
The updated dictionary is: {'name': 'John', 'city': 'New York'}

Example 2:

Enter the key to delete: country
'country' not found in the dictionary.
The updated dictionary is: {'name': 'John', 'age': 25, 'city': 'New York'}

Example 3:

Enter the key to delete: city
'city' has been deleted.
The updated dictionary is: {'name': 'John', 'age': 25}

Conclusion

This Python program demonstrates how to delete a specified key from a dictionary. Deleting keys is an essential operation for managing and maintaining clean data structures in Python programs. This method allows you to remove unwanted data efficiently while ensuring that your dictionary contains only the necessary key-value pairs.

Leave a Comment

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

Scroll to Top