xxxxxxxxxx
# Basic syntax:
del dictionary['key']
# Example usage:
dictionary = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
del dictionary['c'] # Remove the 'c' key:value pair from dictionary
dictionary
--> {'a': 3, 'b': 2, 'd': 4, 'e': 5}
xxxxxxxxxx
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
xxxxxxxxxx
my_dict = {31: 'a', 21: 'b', 14: 'c'}
del my_dict[31]
print(my_dict)
xxxxxxxxxx
# define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# remove a key-value pair from the dictionary
value = my_dict.pop('b')
print(value) #print as 2
print(my_dict) #print as {'a': 1, 'c': 3}
# remove a key-value pair from the dictionary with a default value
value = my_dict.pop('d', None)
print(value) #print as None
print(my_dict) #print as {'a': 1, 'c': 3}
# remove a key-value pair from the dictionary using the `del` statement
del my_dict['a']
print(my_dict) #print as {'c': 3}