user11054331
user11054331

Reputation:

Change the values of a Dictionary for the values of nested dictionaries in it

I have a doubt, and it is if I can make that a dictionary that have dictionaries as values, can take the values of this nested dictionaries and make them its values.

Example:

 d = {"D": {"32_4": 8, "56_7": 4}, "F": {"23_6": 7, "50_7": 0}}

Change it to this:

 {"D": [8, 4], "F": [7, 0]}

So the first dictionary now only contains the values of the nested dictionaries.

Upvotes: 0

Views: 32

Answers (1)

Fukiyel
Fukiyel

Reputation: 1166

You can use this :

d = {"D": {"32_4": 8, "56_7": 4}, "F": {"23_6": 7, "50_7": 0}}
d = {key: list(val.values()) for key, val in d.items()}

Litteraly : "For each key and its corresponding value in d, associate to the key the list of all values in these values"

Upvotes: 1

Related Questions