I am iterating a dict created using the following.
tree = defaultdict(partial(defaultdict, partial(defaultdict, list)))
for dt, hour, value in flat_list:
tree[dt][hour]=[]
tree[dt][hour].append(value)
My output looks like this:
for k,v in tree.iteritems():
2012-08-07 defaultdict(<functools.partial object at 0x1e0a050>, {'17': ['30']})
2012-08-24 defaultdict(<functools.partial object at 0x1e0a050>, {'3': ['70']})
How do I get rid of this stuff? How do I iter like a regular dict?:
defaultdict(<functools.partial object at 0x1e0a050>
You are already iterating over the default dicts like a regular dict, but you are printing the defaultdict representation too.
To print these like you would print a regular dict, just turn them back into one:
Note that a
defaultdictis a direct subclass ofdict, apart from the updated__getitem__behaviour and the updated__repr__hook1, adefaultdictbehaves exactly like a normaldictwould, certainly when it comes to iterating.1
__copy__and__deepcopy__are overridden too, to create a newdefaultdictwhen using thecopymodule. A custom__reduce__is provided for thepicklemodule for the same reasons.