Program to Remove Key from a Dictionary Python || Dictionary || Python


In this we are going to see a basic program to remove key from a dictionary in Python Programming Language.




dict1 = {1: 'a',2: 'b',3: 'c',4: 'd',5: 'e'}
key = int(input("Enter the key to remove: "))

# Method 1

if key in dict1:
    del dict1[key]
    print("Dictionary after removing the key: ",dict1)
else:
    print("Key doesn't exist")

# Method 2

if key in dict1:
    dict1.pop(key)
    print("Dictionary after removing the key: ",dict1)
else:
    print("Key doesn't exist")
 
# Coded By DHIRAJ SHELKE


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post