Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested dictionary iteration
#1
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:]
Reply
#2
You have been previously asked to use python tags when posting code. See the BBCode link in my signature for instructions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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:]
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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:]
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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
Reply
#8
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Nested dictionary acting strange Pedroski55 2 3,645 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 8,528 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 4,501 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  new help with dictionary and dataframe iteration AlphFinan 0 2,259 Oct-13-2020, 11:04 PM
Last Post: AlphFinan
  nested dictionary rkpython 7 5,681 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 5,153 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 2,875 Apr-12-2020, 02:52 PM
Last Post: TomToad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020