By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

idea on how to get/set nested python dictionary values

P: n/a
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

Aug 15 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
....

Aug 15 '06 #2

P: n/a

jojoba wrote:
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
when I add here
ret Data
>
Data = {'a':{'b':{'c':1}}}
paramPath = 'a/b/c'
NewData = 666
SetNewDataParam(Data, paramPath, NewData)
and change this to
ret = SetNewDataParam(Data, paramPath, NewData)
print ret
the shell returns me
{'c': 666}
+++++++++++++++++++++++++++++++++++++++
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
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:

pydef SetNewDataParam2(Data, NewData):
.... if type(Data[Data.keys()[0]]) == type(dict()):
.... SetNewDataParam2(Data[Data.keys()[0]], NewData)
.... else:
.... Data[Data.keys()[0]] = NewData
....
.... return Data
pyData = {'a':{'b':{'c':1}}}
pyNewData = 666
pyret = SetNewDataParam2(Data, NewData)
pyprint ret
{'a': {'b': {'c': 666}}}

Aug 15 '06 #3

P: n/a
hey thank you so much!
that should work fantastically
i like your method much better
more elegant
thanks!

jojoba
=)

Aug 15 '06 #4

P: n/a
wi******@hotmail.com wrote:
pydef SetNewDataParam2(Data, NewData):
... if type(Data[Data.keys()[0]]) == type(dict()):
... SetNewDataParam2(Data[Data.keys()[0]], NewData)
... else:
... Data[Data.keys()[0]] = NewData
...
... return Data
pyData = {'a':{'b':{'c':1}}}
pyNewData = 666
pyret = SetNewDataParam2(Data, NewData)
pyprint ret
{'a': {'b': {'c': 666}}}
This looks better:

def setNested(nest, val):
el = nest.iterkeys().next()
if isinstance(nest[el], dict):
setNested(nest[el], val)
else:
nest[el] = val
return nest

But maybe something like this is closer to the OP needs:

def setNested(nest, path, val):
nest2 = nest
for key in path[:-1]:
nest2 = nest2[key]
nest2[path[-1]] = val
return nest

ndict = {'a1':{'b1':{'c1':1}, "b2":{"c2":2}}}
print ndict
print setNested(ndict, ("a1", "b1", "c1"), 3)
print setNested(ndict, ("a1", "b2", "c2"), 4)

Output:
{'a1': {'b1': {'c1': 1}, 'b2': {'c2': 2}}}
{'a1': {'b1': {'c1': 3}, 'b2': {'c2': 2}}}
{'a1': {'b1': {'c1': 3}, 'b2': {'c2': 4}}}

(But I don't like too much that kind of in place modify.)

Bye,
bearophile

Aug 15 '06 #5

P: n/a
Like for the list.sort() method, to remind you that this function
operate by side effect, maybe it's better if it doesn't return the
modified nested dict:

def setNested(nest, path, val):
nest2 = nest
for key in path[:-1]:
nest2 = nest2[key]
nest2[path[-1]] = val

Bye,
bearophile

Aug 15 '06 #6

P: n/a
wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:

pydef SetNewDataParam2(Data, NewData):
... if type(Data[Data.keys()[0]]) == type(dict()):

Note:

|>type(dict()) is dict
True

"dict" *is* a type...

Aug 15 '06 #7

P: n/a
Once again,
Thanks to all!!!!
I did not expect to receive such a response.
Very very helpful indeed,

jojoba

o(-_-)o

Aug 16 '06 #8

P: n/a

wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:

pydef 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 :)

Aug 16 '06 #9

P: n/a

metaperl wrote:
wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:

pydef 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

Aug 16 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.