def remove_key_from_dict(dictionary, key_to_remove):
"""
Removes a key from the dictionary if it exists.
Args:
dictionary (dict): The dictionary to modify.
key_to_remove: The key to be removed from the dictionary.
Returns:
dict: The modified dictionary.
"""
if key_to_remove in dictionary:
del dictionary[key_to_remove]
print(f"Key '{key_to_remove}' has been removed.")
else:
print(f"Key '{key_to_remove}' not found in the dictionary.")
return dictionary
my_dict = {"name": "Max", "age": 25, "city": "UK"}
print("Original Dictionary:", my_dict)
key = input("Enter the key to remove: ")
updated_dict = remove_key_from_dict(my_dict, key)
print("Updated Dictionary:", updated_dict)
#source code --> clcoding.com
0 Comments:
Post a Comment