I have sort of a similar question as Find matching keys in dictionaries & replace keys with values
However my situation looks like this.
d1 = {"1": "DNS Compromise", "2": "DNS Poisoning", "3": "Fraud Orders"}
d2 = {... ,"categories": [1, 2], ...}
I would like to have the following output:
d2 = {..., "categories": [DNS Compromise, DNS Poisoning], ...}
Is there a way to construct a for loop or use comprehension? I am sort of new to this.
CodePudding user response:
With dictionary you can easily get a list of the values with the method values()
.
d2 = {"categories": d1.values()}
CodePudding user response:
This is the solution you are searching for :
d3 = {k:[ d1[str(v_i)] for v_i in v] for k,v in d2.items() }