Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change all values/Keys in a dictionary

I dont know how to descibe it properly but this is what I want to achieve:

import yaml
list = {"test1":1,"test2":2,"test3":3}
print(yaml.dump(list, sort_keys=False, default_flow_style=False))

#Output
# test1: 1
# test2: 2
# test3: 3

# Update somehow

print(yaml.dump(list, sort_keys=False, default_flow_style=False))

#Output
# <@test1>: 1
# <@test2>: 2
# <@test3>: 3

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use a comprehension to transform your keys:

# list is not a list but a dict and don't use builtin names
data = {"test1":1,"test2":2,"test3":3}

# transform your keys
data = {f'<@{k}>': v for k, v in data.items()}

# export your data as usual
print(yaml.dump(data, sort_keys=False, default_flow_style=False)

Output:

<@test1>: 1
<@test2>: 2
<@test3>: 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading