I'm currently running into a problem with removing a key-value pair from a dictionary object in Python.
Say, a given multi-leveled dictionary is structured like this:
course = {
'name': 'Python',
'release_date': '2014-12-01',
'author': {
'name': 'john',
'email': 'john@gmail.com'
},
'info': {
'instructor': 'john',
'price': 100,
'location': {
'city': 'beijing',
'country': 'china'
}
}
}
I would like to remove the following keys from a list structured like so, with each underscore denoting a new level in the dictionary.
bad_keys = ['release_date', 'author_email', 'info_location_city']
Meaning, the resulting dictionary should be:
course = {
'name': 'Python',
'author': {
'name': 'john',
},
'info': {
'instructor': 'john',
'price': 100,
'location': {
'country': 'china'
}
}
}
What is the most appropriate and / or optimal way of going about this? The key difference between this question and say other questions is the main idea of updating the current dictionary to have the reflected changes of the deleted key-value pairs. Being able to iteratively or recursively iterate through the dictionary isn't a problem, nor is collecting said data--The problem stems from being able to update the current dictionary either while iterating through it or after.