is there a way to add string to all my keys in dictionary without creating a new dictionary? I tried several ways but I get the following error:
for key in result:
RuntimeError: dictionary keys changed during iteration
My code:
def get_temps_servers():
result = dict(re.findall(r"^(.*?) {2,}.*?(\d+C|\d+Watts)", text, flags=re.M))
name = "Test"
for key in result:
x = f"{name} {key}"
result[x] = result[key]
del result[key]
print(result)
return result
This is how my orginal dictionary look:
result = {'CPU1 Temp': '35C', 'CPU2 Temp': '39C', 'System Board Inlet Temp': '18C', 'System Board Exhaust Temp': '29C', 'System Board Pwr Consumption': '130Watts'}
It works great if I create a new dictionary that is copied from the original dictionary, like this one:
def get_temps_servers():
result = dict(re.findall(r"^(.*?) {2,}.*?(\d+C|\d+Watts)", text, flags=re.M))
name = "Test"
res = {f"{name} {key}": val for key, val in result.items()}
print(res)
return res
And this is the result:
res = {'Test CPU1 Temp': '35C', 'Test CPU2 Temp': '39C', 'Test System Board Inlet Temp': '18C', 'Test System Board Exhaust Temp': '29C', 'Test System Board Pwr Consumption': '130Watts'}
Is there a way to get the desired result without creating a new dictionary?
return {f'{name} {key}': val for key, val in re.findall(...)}.