idea on how to get/set nested python dictionary values

jojoba jojoba12 at hotmail.com
Tue Aug 15 13:41:11 EDT 2006


hello!

i am trying to come up with a simple way to access my values in my
nested python dictionaries

here is what i have so far, but i wanted to run it by the geniuses out
there who might see any probems with this...
here is an example:

+++++++++++++++++++++++++++++++++++++++
def SetNewDataParam(Data, paramPath, NewData):
    ParamList  = paramPath.split('/')
    numParams  = len(ParamList)
    for i in range(0, numParams):
        if i != (numParams-1):
            Data = Data[ParamList[i]]
        else:
            Data[ParamList[i]] = NewData


Data          = {'a':{'b':{'c':1}}}
paramPath = 'a/b/c'
NewData    = 666
SetNewDataParam(Data, paramPath, NewData)
+++++++++++++++++++++++++++++++++++++++


so it works!
when i do:
print Data, i get
{'a':{'b':{'c':666}}}


but i am hesistant to be throwing around dictionary references
how is this working????
shouldn't my line above:
Data = Data[ParamList[i]]
screw up my original Data dictionary

Thanks to anyone with comments on this
By the way, i love the idea of using tuples as keys, but my code is so
far along that i dont wanna switch to that elegant way (maybe on a
future project!)
take care,
jojoba




More information about the Python-list mailing list