I want to change the key of an entry in a Python dictionary.
Is there a straightforward way to do this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Easily done in 2 steps:
dictionary[new_key] = dictionary[old_key] del dictionary[old_key]
Or in 1 step:
dictionary[new_key] = dictionary.pop(old_key)
which will raise KeyError if dictionary[old_key] is undefined. Note that this will delete dictionary[old_key].
>>> dictionary = { 1: 'one', 2:'two', 3:'three' }
>>> dictionary['ONE'] = dictionary.pop(1)
>>> dictionary
{2: 'two', 3: 'three', 'ONE': 'one'}
>>> dictionary['ONE'] = dictionary.pop(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
Method 2
if you want to change all the keys:
d = {'x':1, 'y':2, 'z':3}
d1 = {'x':'a', 'y':'b', 'z':'c'}
In [10]: dict((d1[key], value) for (key, value) in d.items())
Out[10]: {'a': 1, 'b': 2, 'c': 3}
if you want to change single key:
You can go with any of the above suggestion.
Method 3
pop’n’fresh
>>>a = {1:2, 3:4}
>>>a[5] = a.pop(1)
>>>a
{3: 4, 5: 2}
>>>
Method 4
In python 2.7 and higher, you can use dictionary comprehension:
This is an example I encountered while reading a CSV using a DictReader. The user had suffixed all the column names with ‘:’
ori_dict = {'key1:' : 1, 'key2:' : 2, 'key3:' : 3}
to get rid of the trailing ‘:’ in the keys:
corrected_dict = { k.replace(':', ''): v for k, v in ori_dict.items() }
Method 5
d = {1:2,3:4}
suppose that we want to change the keys to the list elements p=[‘a’ , ‘b’].
the following code will do:
d=dict(zip(p,list(d.values())))
and we get
{'a': 2, 'b': 4}
Method 6
Since keys are what dictionaries use to lookup values, you can’t really change them. The closest thing you can do is to save the value associated with the old key, delete it, then add a new entry with the replacement key and the saved value. Several of the other answers illustrate different ways this can be accomplished.
Method 7
No direct way to do this, but you can delete-then-assign
d = {1:2,3:4}
d[newKey] = d[1]
del d[1]
or do mass key changes:
d = dict((changeKey(k), v) for k, v in d.items())
Method 8
If you have a complex dict, it means there is a dict or list within the dict:
myDict = {1:"one",2:{3:"three",4:"four"}}
myDict[2][5] = myDict[2].pop(4)
print myDict
Output
{1: 'one', 2: {3: 'three', 5: 'four'}}
Method 9
To convert all the keys in the dictionary
Suppose this is your dictionary:
>>> sample = {'person-id': '3', 'person-name': 'Bob'}
To convert all the dashes to underscores in the sample dictionary key:
>>> sample = {key.replace('-', '_'): sample.pop(key) for key in sample.keys()}
>>> sample
>>> {'person_id': '3', 'person_name': 'Bob'}
Method 10
this function gets a dict, and another dict specifying how to rename keys; it returns a new dict, with renamed keys:
def rekey(inp_dict, keys_replace):
return {keys_replace.get(k, k): v for k, v in inp_dict.items()}
test:
def test_rekey():
assert rekey({'a': 1, "b": 2, "c": 3}, {"b": "beta"}) == {'a': 1, "beta": 2, "c": 3}
Method 11
You can use iff/else dictionary comprehension. This method allows you to replace an arbitrary number of keys in one line.
key_map_dict = {'a':'apple','c':'cat'}
d = {'a':1,'b':2,'c':3}
d = {(key_map_dict[k] if k in key_map_dict else k):v for (k,v) in d.items() }
Returns {'apple':1,'b':2,'cat':3}
Method 12
In case of changing all the keys at once.
Here I am stemming the keys.
a = {'making' : 1, 'jumping' : 2, 'climbing' : 1, 'running' : 2}
b = {ps.stem(w) : a[w] for w in a.keys()}
print(b)
>>> {'climb': 1, 'jump': 2, 'make': 1, 'run': 2} #output
Method 13
This will lowercase all your dict keys. Even if you have nested dict or lists. You can do something similar to apply other transformations.
def lowercase_keys(obj):
if isinstance(obj, dict):
obj = {key.lower(): value for key, value in obj.items()}
for key, value in obj.items():
if isinstance(value, list):
for idx, item in enumerate(value):
value[idx] = lowercase_keys(item)
obj[key] = lowercase_keys(value)
return obj
json_str = {"FOO": "BAR", "BAR": 123, "EMB_LIST": [{"FOO": "bar", "Bar": 123}, {"FOO": "bar", "Bar": 123}], "EMB_DICT": {"FOO": "BAR", "BAR": 123, "EMB_LIST": [{"FOO": "bar", "Bar": 123}, {"FOO": "bar", "Bar": 123}]}}
lowercase_keys(json_str)
Out[0]: {'foo': 'BAR',
'bar': 123,
'emb_list': [{'foo': 'bar', 'bar': 123}, {'foo': 'bar', 'bar': 123}],
'emb_dict': {'foo': 'BAR',
'bar': 123,
'emb_list': [{'foo': 'bar', 'bar': 123}, {'foo': 'bar', 'bar': 123}]}}
Method 14
Replacing spaces in dict keys with underscores, I use this simple route …
for k in dictionary.copy():
if ' ' in k:
dictionary[ k.replace(' ', '_') ] = dictionary.pop(k, 'e r r')
Or just dictionary.pop(k) Note ‘e r r’, which can be any string, would become the new value if the key is not in the dictionary to be able to replace it, which can’t happen here. The argument is optional, in other similar code where KeyError might be hit, that added arg avoids it and yet can create a new key with that ‘e r r’ or whatever you set it to as the value.
.copy() avoids … dictionary changed size during iteration.
.keys() not needed, k is each key, k stands for key in my head.
(I’m using v3.7)
What’s the one-liner for the loop above?
Method 15
You can associate the same value with many keys, or just remove a key and re-add a new key with the same value.
For example, if you have keys->values:
red->1 blue->2 green->4
there’s no reason you can’t add purple->2 or remove red->1 and add orange->1
Method 16
Method if anyone wants to replace all occurrences of the key in a multi-level dictionary.
Function checks if the dictionary has a specific key and then iterates over sub-dictionaries and invokes the function recursively:
def update_keys(old_key,new_key,d):
if isinstance(d,dict):
if old_key in d:
d[new_key] = d[old_key]
del d[old_key]
for key in d:
updateKey(old_key,new_key,d[key])
update_keys('old','new',dictionary)
Method 17
An example of complete solution
Declare a json file which contains mapping you want
{
"old_key_name": "new_key_name",
"old_key_name_2": "new_key_name_2",
}
Load it
with open("<filepath>") as json_file:
format_dict = json.load(json_file)
Create this function to format a dict with your mapping
def format_output(dict_to_format,format_dict):
for row in dict_to_format:
if row in format_dict.keys() and row != format_dict<div class="su-row"></div>:
dict_to_format[format_dict<div class="su-row"></div>] = dict_to_format.pop(row)
return dict_to_format
Method 18
Be aware of the position of pop:
Put the key you want to delete after pop()
orig_dict[‘AAAAA’] = orig_dict.pop(‘A’)
orig_dict = {'A': 1, 'B' : 5, 'C' : 10, 'D' : 15}
# printing initial
print ("original: ", orig_dict)
# changing keys of dictionary
orig_dict['AAAAA'] = orig_dict.pop('A')
# printing final result
print ("Changed: ", str(orig_dict))
Method 19
I wrote this function below where you can change the name of a current key name to a new one.
def change_dictionary_key_name(dict_object, old_name, new_name):
'''
[PARAMETERS]:
dict_object (dict): The object of the dictionary to perform the change
old_name (string): The original name of the key to be changed
new_name (string): The new name of the key
[RETURNS]:
final_obj: The dictionary with the updated key names
Take the dictionary and convert its keys to a list.
Update the list with the new value and then convert the list of the new keys to
a new dictionary
'''
keys_list = list(dict_object.keys())
for i in range(len(keys_list)):
if (keys_list[i] == old_name):
keys_list[i] = new_name
final_obj = dict(zip(keys_list, list(dict_object.values())))
return final_obj
Assuming a JSON you can call it and rename it by the following line:
data = json.load(json_file)
for item in data:
item = change_dictionary_key_name(item, old_key_name, new_key_name)
Conversion from list to dictionary keys has been found here:
https://www.geeksforgeeks.org/python-ways-to-change-keys-in-dictionary/
Method 20
With pandas you can have something like this,
from pandas import DataFrame
df = DataFrame([{"fruit":"apple", "colour":"red"}])
df.rename(columns = {'fruit':'fruit_name'}, inplace = True)
df.to_dict('records')[0]
>>> {'fruit_name': 'apple', 'colour': 'red'}
Method 21
I haven’t seen this exact answer:
dict['key'] = value
You can even do this to object attributes.
Make them into a dictionary by doing this:
dict = vars(obj)
Then you can manipulate the object attributes like you would a dictionary:
dict['attribute'] = value
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0