idea on how to get/set nested python dictionary values

wittempj@hotmail.com martin.witte at gmail.com
Wed Aug 16 11:36:18 EDT 2006


metaperl wrote:
> wittempj at hotmail.com wrote:
>
> > | would use a recursive approach for this - given that you have a sort
> > of recursive datastructure:
> >
> > py> def SetNewDataParam2(Data, NewData):
> > ...     if type(Data[Data.keys()[0]]) == type(dict()):
> > ...         SetNewDataParam2(Data[Data.keys()[0]], NewData)
> > ...     else:
> > ...         Data[Data.keys()[0]] = NewData
> > ...
>
> Data[Data.keys()[0]] is used 3 times in the above code. Is there some
> way to factor out that usage? I'm just starting python but I'm always
> on the lookout for DRY :)

Bearophilehugs gave a much better answer than I did, it also takes away
the need to evaluate the keys() more than one time




More information about the Python-list mailing list