d is a Python dict mapping (some of) the integers in range(x) to values. How can I find which integers in that range are not mapped, and set them to a default value?
I do not want to just the dict’s default, because I want the missing ints to appear in d.items().
>Solution :
Iterate over the range and setdefault each key:
for i in range(x):
d.setdefault(i, 42)
Note that setdefault sets a default value for that key (not the whole dictionary), if and only if that key isn’t already set to something else.
You could also use a comprehension with get and a default value to build a new dictionary and assign it to d:
d = {i: d.get(i, 42) for i in range(x)}