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
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 579 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
  Nested Dictionaries, good programming practice? Curbie 6 2,134 May-28-2025, 10:45 PM
Last Post: Curbie
  Mirroring disk structures in nested dictionaries Curbie 16 4,653 Apr-27-2025, 02:43 PM
Last Post: deanhystad
  Transform 3 Columns into Single Column DaveG 9 4,903 Mar-19-2025, 03:46 AM
Last Post: robbert23
  Transform a pdf to markdown zzied 0 883 Feb-11-2025, 10:22 AM
Last Post: zzied
  Nested Lists & Dictionaries Hudjefa 5 2,258 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
  Sort a list of dictionaries by the only dictionary key Calab 2 2,286 Apr-29-2024, 04:38 PM
Last Post: Calab
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 2,515 Aug-23-2023, 01:21 PM
Last Post: NeilUK

Forum Jump:

User Panel Messages

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