I want to use a defaultdict to initialize a dictionary.
d = collections.defaultdict(set)
for i in range(3):
d[i].add(True)
This would result in a dictionary with keys 1..3
and values of {True}
for each.
Later I want to iterate through the values of this set like so:
for b in d[10]:
print(b)
In a standard dictionary, this would throw because 10
is not a key of the dict. In a default dict, this would initialize 10
with an empty set and not throw.
Is there a way to "lock in" the keys of a defaultdict without copying all of the keys to a new dict? Is d = dict(d)
an O(1)
op?