in Education by
I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2': 2, }, 'foo/bar': { 'opt3': 3, 'opt4': 4, }, 'foo/bar/baz': { 'opt5': 5, 'opt6': 6, } } And I need to get it to look like: { 'foo': { 'opt1': 1, 'opt2': 2, 'bar': { 'opt3': 3, 'opt4': 4, 'baz': { 'opt5': 5, 'opt6': 6, } } } } I should point out that there can and will be multiple top-level keys ('foo' in this case). I could probably throw something together to get what i need, but I was hoping that there is a solution that's more efficient. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Like this: def nest(d): rv = {} for key, value in d.iteritems(): node = rv for part in key.split('/'): node = node.setdefault(part, {}) node.update(value) return rv

Related questions

0 votes
    I'm working on practicing my algorithms and getting into some bitwise stuff which I'm not too proficient ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have the dict as below, if same value is found more than once then my dict key must be created with incremental ... : ['COMPILE'], } Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    How can we delete duplicate rows from flat files?...
asked Mar 26, 2021 in Technology by JackTerrance
0 votes
    Friends, I have a contact us/Inquiry form in my website. I want to generate a lead in Sales force CRM on ... any link for API. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    If the output produces nonconvex regions, then how many layered neural is required at minimum? (a) 2 (b) 3 (c) 4 (d) 5 Please answer the above question....
asked Sep 3, 2022 in Education by JackTerrance
0 votes
    Description: I have a solution in Visual Studio which has multiple projects as application layers. Business Logic ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    Description: I have a solution in Visual Studio which has multiple projects as application layers. Business Logic ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    In layered design functionality and logic is separated into different parts to enable reuse true or false Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    5. Operating System (a) links a program with the subroutines it references (b) provides a layered, user-friendly ... ) all the above Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    In the layered design _____________ is the top most layer. 1. Components 2. Object 3. None of the options 4. Sub process 5. Process...
asked Aug 30, 2021 in Technology by JackTerrance
0 votes
    Suppose you have some subset of codes with you while working with the SVN and you need to move them from one SVN repo ... to another along with their history, how this can be done?...
asked Feb 18, 2021 in Technology by JackTerrance
0 votes
    Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but ... help me with it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Phylum Platyhelminthes and Phylum Aschelminthes. OR Write any two points of differences between flat worms ... proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Which of the following function is used for loading flat files? (a) read.data (b) read.sheet (c) ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    How to make a flat list out of list of lists in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
...