Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, February 2, 2016

python: supply default values for incomplete dictionary



>>> default_dd={'A':1,'B':2,'C':3,'D':4,'E':5}
>>> dd={'B':2000,'D':4000}
>>> for key in default_dd.keys():
...     if not key in dd.keys():
...         dd[key]=default_dd[key]
...       
>>> dd
{'A': 1, 'C': 3, 'B': 2000, 'E': 5, 'D': 4000}
   
   


No comments:

Post a Comment