In python dictionaries, values can be changed arbitrarily; however, keys are unique and direct modification is not supported. If you really need to modify a key in a dictionary, you can do so in several indirect ways.
Create a new blank dictionary.
info = {}
Adds key-value pairs to a dictionary.
info["x"] = 1.5 info["y"] = 2 info
Dictionary keys do not support direct modification.As shown, trying to modify the key directly will report an error.
info = {"x":1.5 ,"y":2} info["z"] = info("x") print(info)
If you need to modify the key value of a dictionary, you need to do it through an indirect method.
The first method: the value corresponding to the key to be modified is extracted using the () method and reassigned to the new key, i.e. dict[new key] = (old key).(The pop of a dictionary dict deletes a key and its corresponding value, and returns the value corresponding to that key.)
info = {"x":1.5 ,"y":2} info["z"] = ("x") info
The second approach: combining the methods of () and ().(The pop of a dictionary dict deletes a key and its corresponding value, and returns the value corresponding to that key.)
info = {"x":1.5 ,"y":2} ({"z":("x")}) info
The third method: a combination of direct modification and del statements.The old key is first assigned to the new key by direct modification, and then the original key name is deleted by the del statement.
info = {"x":1.5 ,"y":2} info["z"] = info["x"] del info["x"] info
Content Expansion
Indirectly modifying the key value of a key method
First (recommended):
dict={'a':1, 'b':2} dict["c"] = ("a")
The second method:
dict={'a':1, 'b':2} ({'c':("a")})
The third method:
dict={'a':1, 'b':2} dict['c']=dict['a'] del dict['a']
This is the whole content of this article.