I want to change the key of an entry in a Python dictionary.
Is there a straightforward way to do this?
Join Stack Overflow to learn, share knowledge, and build your career.
I want to change the key of an entry in a Python dictionary.
Is there a straightforward way to do this?
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
dict[new_value] = dict.pop(old_value, some_default_value)
to avoid that
– Tobias Kienzler
Jul 31 '13 at 10:59
old_key
will be different from the position of new_key
.
– norok2
Jan 24 '20 at 20:16
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.
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() }
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.
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'}}
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())
d = dict(...)
and d = {...}
are the same things. There is a another comment from 2013 that suggest the same change to another answer. So I'm assuming they must not be the same, and that they must differ in some meaningful way. What is that way?
– Unknow0059
Nov 22 '20 at 0:52
dict()
behaves when passed a generator objects vs. how {...}
behaves. For some direction on reading I would say start here: python.org/dev/peps/pep-0274
– Erich
Nov 23 '20 at 1:10
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}
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'}
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}
print(inp_dict)
instead of that assert
. Still, better than the alternative.
– Unknow0059
Nov 22 '20 at 1:52
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
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}]}}
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 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)
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[row]:
dict_to_format[format_dict[row]] = dict_to_format.pop(row)
return dict_to_format
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))
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/
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