'Which is the best way to represent my data

I have a following data structure:

Page0
    Key0
        Text
        Color
        Name
    Key1
        Text
        Color
        Name
Page1    
    Key0
        Text
        Color
        Name
    Key1
        Text
        Color
        Name

Which is the best data structure to use ?



Solution 1:[1]

Options:

Tree
Nested dictionaries
Nested lists
Dataframe with multi-index
Custom classes (Class Page has attribute of class Key, class Key has attributes Text, Color, Name)

Which one is best depends on the use case, especially how you're going to be accessing it (both reads and writes). A dataframe with multi-index is the more "cube" like option. People who are into object oriented programming would probably go for the custom classes option. Nested lists/dictionaries are in some sense the "lightest" option, and are more amenable to export options such as JSON, and if your code accessing is itself nested loops, this may be a good match, but it may get unwieldy if you want to visually inspect the structure. Lists take up less space in memory, but can be slower to access, and aren't as nicely labeled.

Solution 2:[2]

I would go with a nested dictionary structure like this:

myThings = {
    "Page0": {
        "Key0": {
            Text,
            Color,
            Name
        },
        "Key1": {
            Text,
            Color,
            Name
        }
    },
    "Page1": {    
        "Key0": {
            Text,
            Color,
            Name
        },
        "Key1": {
            Text,
            Color,
            Name
        }
    }
}

Solution 3:[3]

There's a variety of options, depending on your usage of the objects, and mainly on how you want to access the objects after they are created. There is not one correct answer, and here are some optional guidelines.

Each of your objects (Keys and Pages) can be described as classes.

If your objects have methods that change their data than you can implement them as a class, but if they don't have methods, and just hold the info, you can implement them as dataclass (which has automatic generic method implementation). If you really don't want a class and you don't have methods at all, you can put the data at a namedtuple or just a dict, but then you'll access the members of your objects by strings.

For accessing the objects after creation there are some options:

  1. If you just want to hold the objects, and the order doesn't matter, that means that for each access you go over all of your objects, you can use a set.
  2. If you are going to access your objects serially or their order does matter, you can put them in a list.
  3. If each of your objects has a name (id) and you want to access each of the objects specifically, you can use a dict.

Of course there are more considerations, like runtime and memory, but that can be optimized after you chose the basics.

In your case you have to choose 2 collections - one for Keys for each Page, and one for all of the pages.

For example if the order of the keys in the page doesn't matter, but the order of the pages does, with the current information from ykur case, I would implement your classes as dataclass, and each Page will hold a set of Keys, and all of the Pages would be held in a list of Pages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Acccumulation
Solution 2 grepgrok
Solution 3 user107511