;

Python Program to Remove Given Key from a Dictionary


Tutorialsrack 06/05/2020 Python

In this Python program, we will learn how to remove a given key from a dictionary. In this program, we used the three ways to remove the key from the dictionary. The first way is, we used the del statement to remove a given key from a dictionary. The second way is to use the del statement with keys() function and the third way is to use the pop() function with the keys() function.

Here is the source code of the program to remove a given key from a dictionary. 

Program 1: Python Program to Remove Given Key From a Dictionary Using del function to delete key-value from a given dictionary

In this program, we used the del statement to remove the key from the given dictionary. 

Python Program to Remove Given Key From a Dictionary Using del function to delete key-value from a given dictionary
# Python Program to Remove Given Key From a Dictionary
# Using del function to delete key-value from a given dictionary

# Declare the Dictionary
myDict = {'Name': 'Tom', 'Age': 25, 'Designation': 'Developer'}

# Print the Dictionary
print("Dictionary Items: ",  myDict)

# Take the input from the user
key = input("\nEnter the key that you want to Delete: ")

if key in myDict:
    del myDict[key]
else:
    print("Given Key is Not in this Dictionary")
    
# Print the Dictionary After Removing Key    
print("\nUpdated Dictionary = ", myDict)
Output

Dictionary Items:  {'Name': 'Tom', 'Age': 25, 'Designation': 'Developer'}

 

Enter the key that you want to Delete: Age

 

Updated Dictionary =  {'Name': 'Tom', 'Designation': 'Developer'}

Program 2: Python Program to Remove Given Key From a Dictionary Using Keys() function with del function to delete key-value from a given dictionary

In this program, we used the python built-in keys() function to get the list of all the items in a dictionary and del statement to remove the key from the given dictionary.

Python Program to Remove Given Key From a Dictionary Using Keys() function with del function to delete key-value from a given dictionary
# Python Program to Remove Given Key From a Dictionary
# Using Keys() function with del function to delete key-value from a given dictionary

# Declare the Dictionary
myDict = {'Name': 'Tom', 'Age': 25, 'Designation': 'Developer'}

# Print the Dictionary
print("Dictionary Items: ",  myDict)

# Take the Input From the User
key = input("Enter the key that you want to Delete: ")

if key in myDict.keys():
    del myDict[key]
else:
    print("Given Key is Not in this Dictionary")
    
# Print the Dictionary After Removing Key    
print("\nUpdated Dictionary = ", myDict)
Output

Dictionary Items:  {'Name': 'Tom', 'Age': 25, 'Designation': 'Developer'}

Enter the key that you want to Delete: Name

 

Updated Dictionary =  {'Age': 25, 'Designation': 'Developer'}

Program 3: Python Program to Remove Given Key From a Dictionary Using keys() and pop() function to delete key-value from a given dictionary

In this program, we used the python built-in keys() function which returns the lists of keys from a dictionary. And the pop() function is used to remove the key from the dictionary.

Python Program to Remove Given Key From a Dictionary Using keys() and pop() function to delete key-value from a given dictionary
# Python Program to Remove Given Key From a Dictionary
# Using keys() and pop() function to delete key-value from a given dictionary

# Declare the Dictionary
myDict = {'name': 'John', 'age': 25, 'job': 'Developer'}

# Print the Dictionary
print("Dictionary Items: ",  myDict)

# Take the Input From the User
key = input("Please enter the key that you want to Delete : ")

if key in myDict.keys():
    print("Removed Item  :  ", myDict.pop(key))
else:
    print("Given Key is Not in this Dictionary")
    
# Print the Dictionary After Removing Key    
print("\nUpdated Dictionary = ", myDict)  
Output

Dictionary Items:  {'name': 'John', 'age': 25, 'job': 'Developer'}

Please enter the key that you want to Delete : job

Removed Item  :   Developer

 

Updated Dictionary =  {'name': 'John', 'age': 25}


Related Posts



Comments

Recent Posts
Tags