Python Forum

Full Version: nested dictionary iteration
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
d is the dictionary...
def printDict(d):
for oldkey, v in d.iteritems():
if type(v) is dict:
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
printDict(v)
else:
for oldkey, w in d.iteritems():
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]

def camel_case(string)
title=string.title().replace("_","")
ca,el=title[0].lower+title[1:]
You have been previously asked to use python tags when posting code. See the BBCode link in my signature for instructions.
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
d is the dictionary...
def printDict(d):
for oldkey, v in d.iteritems():
if type(v) is dict:
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
printDict(v)
else:
for oldkey, w in d.iteritems():
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]

def camel_case(string)
title=string.title().replace("_","")
ca,el=title[0].lower+title[1:]
When you post the code, you need to make sure your copy/paste maintained the indentation of the code. Please reread the instructions.

That said, you are modifying the dictionary as you are iterating over it. That is a bad idea. You should build a new dictionary based on the current dictionary. If the key is appropriate, copy it's value over. If the key is not appropriate, change the key and copy the value into the new key.
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
def printDict(d):
  for oldkey, v in d.iteritems():
     if type(v) is dict:
        newkey=camel_case(old_key)
        d[newkey]=d.pop[oldkey]
        printDict(v)
     else:
        for oldkey, w in d.iteritems():
            newkey=camel_case(old_key)
            d[newkey]=d.pop[oldkey]
 
def camel_case(string)
  title=string.title().replace("_","")
  camel=title[0].lower+title[1:]
Have you even tried to run this code? There are errors all over it. You have no colon at the end of line 12, and you refer to oldkey two different ways. Solve the basic problems first. Get it running with a test case, if it gives the wrong output, come back and tell us exactly how the output is wrong.
(Sep-02-2018, 05:00 PM)ichabod801 Wrote: [ -> ]Have you even tried to run this code? There are errors all over it. You have no colon at the end of line 12, and you refer to oldkey two different ways. Solve the basic problems first. Get it running with a test case, if it gives the wrong output, come back and tell us exactly how the output is wrong.

The code is working fine.if u have any logic for my ques ,please suggest
(Sep-02-2018, 05:54 PM)saisankalpj Wrote: [ -> ]The code is working fine.if u have any logic for my ques ,please suggest
No, it does not. As Ichabood has said there are errors.
(Sep-02-2018, 05:54 PM)saisankalpj Wrote: [ -> ]The code is working fine.if u have any logic for my ques ,please suggest

I did suggest, and you ignored my suggestion.