2
Good afternoon, I have a big keyError problem, because in my program that makes use of Dict will perform several processes, and if at some point he does not find an item in which he looks, will generate me a keyError. So I made a simple function that could help me in this case.However I don’t see a way to standardize this function created to use in several dicts, just passing the key values to make such a change. Working: It assigns a new item to another item that already contains value. After that it will delete the old item I want to replace.
The function was used to adjust the following test Dict, x = {'Name':'Mark','Age':21}
def Test(dictionary):
dictionary['Name1'] = dictionary['Name']
del dictionary['Name']
return dictionary
print(Test(x))
{'Name1':'Mark', 'Age':21}
I would like it to be in a format that can be used for any case.
def Test(dictionary):
newkey = oldkey
del oldkey
return dictionary
The cool thing is that there are still numerous ways to apply this. How to use the parameter
**kwargs*
, calling the function withChangeDictKey(dictionary, Name='Original Name', Age='Marriage Age')
.– Breno