Python Forum
Transform simplified dictionary to nested dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transform simplified dictionary to nested dictionaries
#1
Today, I was trying to solve a question in which the data is structured like:
{
  "bar": [
    {
      "id": 1,
      "foo_id": 1,
      "title": "bar title 1"
    },
    ...
  ],
  "foo": [
    {
      "id": 1,
      "title": "foo title 1"
    },
    {
      "id": 2,
      "title": "foo title 2"
    },
    ...
  ],
  "baz": [
    {
      "id": 1,
      "bar_id": 1,
      "title": "baz title 1"
    },
    ...
  ]
}
And it should be transformed in this format (no id, foo_id, bar_id will be included):
{
  "foo title 1": [
    {
      "bar title 1": [
        {
          "final": "baz title 1", // take baz title as "final"
          "query": "done" // this is always done
        },
        ...
      ],
      ...
    },
    ...
  ],
  ...
}
The dictionary of bar list matches with the dictionary of foo list. The dictionary of baz list matches with the dictionary of bar list.

And I solved this with a lot of for loops and with more than 30 lines of code. But I think this can be solved with no more than 2 loops. I'll be glad to see the code shorter as far as possible.

Your help will let me learn to code in better way. And also, what kind of articles, posts, sites to practice on, should I read more for improving my skill on data structures.

Thanks.
Reply
#2
It looks like three loops to me. First loop through the foos, adding the title and an empty list to the master dictionary. Then loop through the bars, appending a dictionary (with the title and an empty list) to the list in the appropriate foo. In this loop you also need to keep track of another dictionary of bar ids to foos (the title keys in the master dictionary). Finally, loop through the bazes, adding them to the appropriate master[foo][bar] list, using your dictionary of bars to foos.
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
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 553 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Transform Dic to dataframe d9d9d 4 1,343 Apr-14-2022, 09:35 AM
Last Post: perfringo
  Transform 3 Columns into Single Column DaveG 8 1,801 Apr-04-2022, 08:42 AM
Last Post: Pedroski55
  How to transform from wide to long format in python shantanu97 1 1,614 Nov-21-2021, 11:53 AM
Last Post: buran
  Nested dictionary acting strange Pedroski55 2 2,054 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,426 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,560 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  convert List with dictionaries to a single dictionary iamaghost 3 2,801 Jan-22-2021, 03:56 PM
Last Post: iamaghost

Forum Jump:

User Panel Messages

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