0
dict_A = {
    'Key A': Value1,
    'Key B': Value2
}

dict_B = {
    Value1: ValueX,
    Value2: ValueY
}

How can I replace values in dict_A by a value from dict_B when value-key are matched?

Rafał
  • 51
  • 10

2 Answers2

1

You can use dict.get():

dict_A = {"Key A": "Value1", "Key B": "Value2"}

dict_B = {"Value1": "ValueX", "Value2": "ValueY"}

for key, value in dict_A.items():
    dict_A[key] = dict_B.get(value, value)

print(dict_A)

Prints:

{'Key A': 'ValueX', 'Key B': 'ValueY'}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1
dict_A = {k: dict_B.get(v, v) for k, v in dict_A.items()}
qwerty
  • 887
  • 11
  • 33
bobah
  • 18,364
  • 2
  • 37
  • 70