How to change all values/Keys in a dictionary

Question:

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
Asked By: Boss Limette

||

Answers:

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
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.