Reputation: 11
I'm trying to convert all values of some dictionaries that are nested in another dictionary.
I want to convert:
{0: {'n': 1}, 1: {'s': 0, 'n': 2}, 2: {'s': 1}}
To this:
{0: {'n': '?'}, 1: {'s': '?', 'n': '?'}, 2: {'s': '?'}}
I tried this:
for key, value in new_dictt:
new_dictt[key][value] = '?'
But it did not work. I've been googling but have not found a way to convert all values of all dictionaries within another dictionary.
Upvotes: 1
Views: 60
Reputation: 43169
Here we go:
old_dict = {0: {'n': 1}, 1: {'s': 0, 'n': 2}, 2: {'s': 1}}
new_dict = {key: {k: '?' for k in dct} for key, dct in old_dict.items()}
print(new_dict)
Which yields
{0: {'n': '?'}, 1: {'s': '?', 'n': '?'}, 2: {'s': '?'}}
This uses two nested dict comprehensions.
Upvotes: 7