mydict[k_new] = mydict.pop(k_old)
def rename_keys(dict_, new_keys):
"""
new_keys: type List(), must match length of dict_
"""
# dict_ = {oldK: value}
# d1={oldK:newK,} maps old keys to the new ones:
d1 = dict( zip( list(dict_.keys()), new_keys) )
# d1{oldK} == new_key
return {d1[oldK]: value for oldK, value in dict_.items()}
# For a regular dict
mydict[k_new] = mydict.pop(k_old)
# To preserve ordering
d = {0:0, 1:1, 2:2, 3:3}
{"two" if k == 2 else k:v for k,v in d.items()}
# => {0: 0, 1: 1, 'two': 2, 3: 3}