My dict is a dict of {str: {str: list of str}}
ex:
{'hii':{'bye': [1, 2]}}
what i want:
{'hi':{'bye': [1, 2]}}
Is there a way to change the 'hii'
to just 'hi'
?
what I've tried only edits the values and not the keys.
My dict is a dict of {str: {str: list of str}}
ex:
{'hii':{'bye': [1, 2]}}
what i want:
{'hi':{'bye': [1, 2]}}
Is there a way to change the 'hii'
to just 'hi'
?
what I've tried only edits the values and not the keys.
You do need to remove and re-add, but you can do it one go with pop
:
d['hi'] = d.pop('hii')
You need to remove the old key/value pair and insert a new one:
d = {'hii': {'bye': [1, 2]}}
d['hi'] = d['hii']
del d['hii']
You cannot change a key in a dictionary, because the key object must be hashable, and therefore immutable. Daniel Roseman's answer looks like the most elegant way of accomplishing your goal.