Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to group similar items into a python list of dictionaries for HTML ul list

I am trying to create an HTML selected menu. I have the following python data structure:

categories = [
        ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters']
        ['Toys & Games', 'Learning & Education', 'Science Kits & Toys']
        ['Toys & Games', 'Arts & Crafts', 'Craft Kits']
        ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons']
        ['Home & Kitchen', 'Home Décor', 'Window Treatments', 'Window Stickers & Films']
    ]

However to get this to work with the HTML/JQuery menu, I need to change the above data structure to this:

categories = [
            {
                "name": 'Sports & Outdoors',
                "subcategory": [
                    {
                        "name": 'Outdoor Recreation',
                        "subcategory": [
                            {
                                "name": 'Skates, Skateboards & Scooters',
                                "last": True
                            }
                        ]
                    }
                ]
            },
            {
            "name": 'Toys & Games',
            "subcategory": [
                {
                    "name": 'Learning & Education',
                    "subcategory": [
                        {
                        "name": 'Science Kits & Toys',
                        "last": True
                        }
                    ]
                },
                {
                    "name": 'Arts & Crafts',
                    "subcategory": [
                        {
                            "name": 'Craft Kits',
                            "last": True
                        },
                        {
                            "name": 'Drawing & Painting Supplies',
                            "subcategory": [
                                {
                                    "name": 'Crayons',
                                    "last": True
                                }
                            ]
                        }
                    ]
                },
                
            ]
                
            },
            {
                "name": 'Home & Kitchen',
                "subcategory": [
                    {
                    "name": 'Home Décor',
                        "subcategory": [
                            {
                                "name": 'Window Treatments',
                                "subcategory": [
                                    {
                                        "name": 'Window Stickers & Films',
                                        "last": True
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]

I can’t get to wrap my head around it. Any help is appreciated. How can I convert the python list of list, to a list of dictionary items as shown above, at the same time doing the grouping? If possible at the end of the tree, indicating with a "last": True.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try this:

categories = [
        ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters'],
        ['Toys & Games', 'Learning & Education', 'Science Kits & Toys'],
        ['Toys & Games', 'Arts & Crafts', 'Craft Kits'],
        ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons'],
        ['Home & Kitchen', 'Home Decor', 'Window Treatments', 'Window Stickers & Films']
    ]

new_categories = []
for category in categories:
    existing_category = next((c for c in new_categories if c["name"] == category[0]), None)
    if existing_category:
        new_category = existing_category
    else:
        new_category = {"name": category[0], "subcategory": []}
        new_categories.append(new_category)
    
    last_subcategory = new_category
    for subcategory in category[1:]:
        existing_subcategory = next((c for c in last_subcategory["subcategory"] if c["name"] == subcategory), None)
        if existing_subcategory:
            new_subcategory = existing_subcategory
        else:
            new_subcategory = {"name": subcategory, "subcategory": []}
            last_subcategory["subcategory"].append(new_subcategory)
        last_subcategory = new_subcategory
    last_subcategory["last"] = True


print(new_categories)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading