Rename a dictionary key

后端 未结 12 861
盖世英雄少女心
盖世英雄少女心 2020-11-22 16:54

Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value?

I

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 17:54

    Suppose you want to rename key k3 to k4:

    temp_dict = {'k1':'v1', 'k2':'v2', 'k3':'v3'}
    temp_dict['k4']= temp_dict.pop('k3')
    

提交回复
热议问题