How to build defaultdict from a dictionary?

If I have d=dict(zip(range(1,10),range(50,61))) , how can I build collections.defaultdict from dict ?

It seems that the only argument to defaultdict is the factory function, do I need to initialize and then go through the original d and update defaultdict ?

+42
python dictionary
Sep 24 2018-11-12T00:
source share
1 answer

Read the docs :

The first argument provides the initial value for the default_factory attribute; the default value is None. All other arguments are treated as if they were passed to the dict constructor , including keywords.

 from collections import defaultdict d=defaultdict(int, zip(range(1,10),range(50,61))) 

Or the d dictionary is given:

 from collections import defaultdict d=dict(zip(range(1,10),range(50,61))) my_default_dict = defaultdict(int,d) 
+44
Sep 24 '11 at 12:50
source share



All Articles