Replace a dictionary key from a list
Question:
Let’s say I have the following list:
my_list= ['aa', 'bb', 'cc']
My dictionary:
my_dict = {'source':{'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
I would like to replace the new_key
value in the dictionary with the list value my_list
and I would like to get the following three dictionaries
{'source':{'aa': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source':{'bb': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source':{'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}
I have tried the following
for i in my_list:
my_dict ['source'][f'{i}'] = my_dict ['sources']['new_key']
del my_dict ['source']['new_key']
Answers:
The issue is that you keep modifying the only existing dict instance, but you need new dict, and keep the original one as base
You need to make a deep copy of the original, modify it, and save it
from copy import deepcopy
my_list = ['aa', 'bb', 'cc']
my_dict = {'source': {'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
result = []
for item in my_list:
value = deepcopy(my_dict)
value['source'][item] = value['source']['new_key']
del value['source']['new_key']
result.append(value)
print(result)
[{'source': {'aa': {'k1': 1, 'k2': 2, 'k3': 3}}},
{'source': {'bb': {'k1': 1, 'k2': 2, 'k3': 3}}},
{'source': {'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}]
If there is only this change to do, you can simplify the code
result = []
for item in my_list:
result.append({'source': {item: deepcopy(my_dict['source']['new_key'])}})
You need to make a deep copy of the dictionary to be able to modify each of them separately
from copy import deepcopy
my_list= ['aa', 'bb', 'cc']
my_dict = {'source': {'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
outdicts = {}
for l in my_list:
outdicts[l] = deepcopy(my_dict)
outdicts[l]['source'][l] = outdicts[l]['source'].pop('new_key')
print(f"{outdicts['aa'] = }")
print(f"{outdicts['bb'] = }")
print(f"{outdicts['cc'] = }")
Here’s how you create the dictionaries. What you do with them is up to you:
USE_DEEPCOPY = False # change as needed
if USE_DEEPCOPY:
from copy import deepcopy
else:
def deepcopy(v):
return v
my_dict = {'source':{'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
my_list= ['aa', 'bb', 'cc']
v = my_dict['source']['new_key']
for k in my_list:
d = {'source':{k: deepcopy(v)}}
print(d)
Output:
{'source': {'aa': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source': {'bb': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source': {'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}
Let’s say I have the following list:
my_list= ['aa', 'bb', 'cc']
My dictionary:
my_dict = {'source':{'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
I would like to replace the new_key
value in the dictionary with the list value my_list
and I would like to get the following three dictionaries
{'source':{'aa': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source':{'bb': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source':{'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}
I have tried the following
for i in my_list:
my_dict ['source'][f'{i}'] = my_dict ['sources']['new_key']
del my_dict ['source']['new_key']
The issue is that you keep modifying the only existing dict instance, but you need new dict, and keep the original one as base
You need to make a deep copy of the original, modify it, and save it
from copy import deepcopy
my_list = ['aa', 'bb', 'cc']
my_dict = {'source': {'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
result = []
for item in my_list:
value = deepcopy(my_dict)
value['source'][item] = value['source']['new_key']
del value['source']['new_key']
result.append(value)
print(result)
[{'source': {'aa': {'k1': 1, 'k2': 2, 'k3': 3}}},
{'source': {'bb': {'k1': 1, 'k2': 2, 'k3': 3}}},
{'source': {'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}]
If there is only this change to do, you can simplify the code
result = []
for item in my_list:
result.append({'source': {item: deepcopy(my_dict['source']['new_key'])}})
You need to make a deep copy of the dictionary to be able to modify each of them separately
from copy import deepcopy
my_list= ['aa', 'bb', 'cc']
my_dict = {'source': {'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
outdicts = {}
for l in my_list:
outdicts[l] = deepcopy(my_dict)
outdicts[l]['source'][l] = outdicts[l]['source'].pop('new_key')
print(f"{outdicts['aa'] = }")
print(f"{outdicts['bb'] = }")
print(f"{outdicts['cc'] = }")
Here’s how you create the dictionaries. What you do with them is up to you:
USE_DEEPCOPY = False # change as needed
if USE_DEEPCOPY:
from copy import deepcopy
else:
def deepcopy(v):
return v
my_dict = {'source':{'new_key': {'k1': 1, 'k2': 2, 'k3': 3}}}
my_list= ['aa', 'bb', 'cc']
v = my_dict['source']['new_key']
for k in my_list:
d = {'source':{k: deepcopy(v)}}
print(d)
Output:
{'source': {'aa': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source': {'bb': {'k1': 1, 'k2': 2, 'k3': 3}}}
{'source': {'cc': {'k1': 1, 'k2': 2, 'k3': 3}}}