To remove a key-value pair from a dictionary in Python, you can use the `del` statement or the `pop` method. Both methods remove the element with the specified key from the dictionary.
We can remove a key-value pair from a dictionary in Python using the `del` statement. This example demonstrates how to remove a key-value pair by specifying the key and print the dictionary contents before and after the removal.
For example,
my_dict
with integer keys and string values.my_dict = {
1: 'One',
2: 'Two',
3: 'Three'
}
# Print the dictionary contents before removal
print('Dictionary contents before removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
# Key to remove
key_to_remove = 2
# Remove the key-value pair using the del statement
del my_dict[key_to_remove]
# Print the dictionary contents after removal
print('Dictionary contents after removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents before removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Dictionary contents after removal: Key: 1, Value: One Key: 3, Value: Three
We can remove a key-value pair from a dictionary in Python using the `pop` method. This example demonstrates how to remove a key-value pair by specifying the key and print the dictionary contents before and after the removal.
For example,
my_dict
with integer keys and string values.my_dict = {
1: 'One',
2: 'Two',
3: 'Three'
}
# Print the dictionary contents before removal
print('Dictionary contents before removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
# Key to remove
key_to_remove = 2
# Remove the key-value pair using the pop method
my_dict.pop(key_to_remove)
# Print the dictionary contents after removal
print('Dictionary contents after removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents before removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Dictionary contents after removal: Key: 1, Value: One Key: 3, Value: Three
We can check if a key exists before attempting to remove a key-value pair in a dictionary in Python using the `pop` method. This example demonstrates how to check for key existence and remove the key-value pair if it exists, then print the dictionary contents before and after the removal.
For example,
my_dict
with integer keys and string values.my_dict = {
1: 'One',
2: 'Two',
3: 'Three'
}
# Print the dictionary contents before removal
print('Dictionary contents before removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
# Key to remove
key_to_remove = 4
# Check if the key exists and remove it if it does
if key_to_remove in my_dict:
my_dict.pop(key_to_remove)
print(f'Key {key_to_remove} was removed from the dictionary.')
else:
print(f'Key {key_to_remove} does not exist in the dictionary.')
# Print the dictionary contents after removal
print('Dictionary contents after removal:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents before removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key 4 does not exist in the dictionary. Dictionary contents after removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
In this tutorial, we learned How to Remove a Key-Value Pair from a Dictionary in Python language with well detailed examples.