Build a new dict with a comprehension:
>>> x={6:{"Apple","Banana","Tomato"},9:{"Cake"},11:{"Pineapple","Apple"}}
>>> x = {k: x.get(k, "") for k in range(1, max(x)+1)}
>>> x
{1: '', 2: '', 3: '', 4: '', 5: '', 6: {'Banana', 'Tomato', 'Apple'}, 7: '', 8: '', 9: {'Cake'}, 10: '', 11: {'Apple', 'Pineapple'}}
Depending on your use case, you might also find defaultdict
useful, e.g.:
>>> from collections import defaultdict
>>> x = defaultdict(str)
>>> x.update({6:{"Apple","Banana","Tomato"},9:{"Cake"},11:{"Pineapple","Apple"}})
>>> x[6]
{'Banana', 'Tomato', 'Apple'}
>>> x[1]
''
The idea with defaultdict
is that any key you try to access will provide the default (in this case str()
) if no other value has been set -- there's no need to go and fill in the "missing" values ahead of time because the dictionary will just supply them as needed. The case where this wouldn't work would be if you needed to iterate over the entire dictionary and have those empty values be included.