my_score = {
"math": 90,
"physics": 80,
"chemistry": 85,
"english": 91
}
#TO REMOVE 'physics' kEY from my_score
del my_score['physics']
Output
{'math': 90, 'chemistry': 85, 'english': 91}
To delete a key from a dictionary in python you can use the del statment before the dictionary variable name.
Syntax
dict['key_name']
Code example
user_info = {
'id': 2546,
'name': 'Mohit',
'country': 'India',
'city': 'Delhi'
}
del user_info['country']
print(user_info)
Output
{'id': 2546, 'name': 'Mohit', 'city': 'Delhi'}
0 Comments