Home > OS >  How to change all values/Keys in a dictionary
How to change all values/Keys in a dictionary

Time:01-27

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

CodePudding user response:

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
  • Related