I want to change the key of an entry in a Python dictionary.
Is there a straightforward way to do this?
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
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.
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}
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())
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}
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}
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}]}}
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?
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/
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'}
I just had to help my wife do something like those for a python class, so I made this code to show her how to do it. Just like the title says, it only replaces a key name. It's very rare that you have to replace just a key name, and keep the order of the dictionary intact but figured I'd share anyway since this post is what Goggle returns when you search for it even though it's a very old thread.
Code:
dictionary = {
"cat": "meow",
"dog": "woof",
"cow": "ding ding ding",
"goat": "beh"
}
def countKeys(dictionary):
num = 0
for key, value in dictionary.items():
num += 1
return num
def keyPosition(dictionary, search):
num = 0
for key, value in dictionary.items():
if key == search:
return num
num += 1
def replaceKey(dictionary, position, newKey):
num = 0
updatedDictionary = {}
for key, value in dictionary.items():
if num == position:
updatedDictionary.update({newKey: value})
else:
updatedDictionary.update({key: value})
num += 1
return updatedDictionary
for x in dictionary:
print("A", x, "goes", dictionary[x])
numKeys = countKeys(dictionary)
print("There are", numKeys, "animals in this list.\n")
print("Woops, that's not what a cow says...")
keyPos = keyPosition(dictionary, "cow")
print("Cow is in the", keyPos, "position, lets put a fox there instead...\n")
dictionary = replaceKey(dictionary, keyPos, "fox")
for x in dictionary:
print("A", x, "goes", dictionary[x])
Output:
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.
Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...
A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
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