dictionary - How can make all dictionaries inside list of dictionaries Default in Python -


suppose have list of dictionaries this

dlist = [d1, d2, d3, d4] d1 inturn dictionaries of dictionaries like

d1 = {'dd1':{'a':2. 'ddd1':'moredict'}}

and inside can many more dictionaries

is there single line function can convert dictionaries deafult.

i want if key don't exist in of child dict don't key error.

edit:

#something , looking buildin def convert_dict(dictionary):     key, value in dictionary.iteritems():         if isinstance(value, dict):             dictionary[key] = defaultdict(list, value)             convert_dict(value) 

i want if key don't exist in of child dict don't key error.

setting defaults 1 option this, or use get:

>>> d = {'key': 1} >>> d['foo'] traceback (most recent call last):   file "<stdin>", line 1, in <module> keyerror: 'foo' >>> d.get('foo') 

get return none if key doesn't exist, instead of raising keyerror. can have return default value other none, passing in:

>>> d.get('foo',{}) {} 

Comments