Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 873765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:59:39+00:00 2026-05-15T10:59:39+00:00

To generate a Table of Content, I have these data available in a Python

  • 0

To generate a Table of Content, I have these data available in a Python list:

data = [
    {title: 'Section 1', level: 1, page_number: 1},
    {title: 'Section 1.1', level: 2, page_number: 2},
    {title: 'Section 1.2', level: 2, page_number: 3},
    {title: 'Section 2', level: 1, page_number: 4},
    {title: 'Section 2.1', level: 2, page_number: 5},
    {title: 'Section 3', level: 1, page_number: 6},
]

From this, I’d like to obtain this kind of nested structure, much more compatible with the use of a template engine:

toc = [
    {title: 'Section 1', page_number: 1, sub: [
        {title: 'Section 1.1', page_number: 2, sub: []},
        {title: 'Section 1.2', page_number: 3, sub: []},
    ]},
    {title: 'Section 2', page_number: 4, sub: [
        {title: 'Section 2.1', page_number: 5, sub: []},    
    ]},
    {title: 'Section 3', page_number: 6, sub: []},
]

Hints on how to achieve this? I tried with a recursive function but it’s getting much tricky for my limited brain.

Any help much appreciated.

EDIT: Added fact that a section entry can have eventually no child. Sorry for the miss.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:59:40+00:00Added an answer on May 15, 2026 at 10:59 am

    Assuming chapters come in order, meaning child chapter is always after the parent, and there are no missing parent (skipped levels):

    import pprint
    
    data = [
        {'title': 'Section 1', 'level': 1, 'page_number': 1},
        {'title': 'Section 1.1', 'level': 2, 'page_number': 2},
        {'title': 'Section 1.2', 'level': 2, 'page_number': 3},
        {'title': 'Section 2', 'level': 1, 'page_number': 4},
        {'title': 'Section 2.1', 'level': 2, 'page_number': 42},
        {'title': 'Section 2.1.1', 'level': 3, 'page_number': 42},
        {'title': 'Section 3', 'level': 1, 'page_number': 42},
    ]
    
    toc = []
    stack = [toc]
    for d in data:
        d['sub'] = []   
        while d['level'] < len(stack):
            stack.pop()
        while d['level']  > len(stack):
            stack.append(stack[-1][-1]['sub'])
        stack[-1].append(d)
    
    
    pprint.pprint(toc)
    

    Result:

    [{'level': 1,
      'page_number': 1,
      'sub': [{'level': 2, 'page_number': 2, 'sub': [], 'title': 'Section 1.1'},
              {'level': 2, 'page_number': 3, 'sub': [], 'title': 'Section 1.2'}],
      'title': 'Section 1'},
     {'level': 1,
      'page_number': 4,
      'sub': [{'level': 2,
               'page_number': 42,
               'sub': [{'level': 3,
                        'page_number': 42,
                        'sub': [],
                        'title': 'Section 2.1.1'}],
               'title': 'Section 2.1'}],
      'title': 'Section 2'},
     {'level': 1, 'page_number': 42, 'sub': [], 'title': 'Section 3'}]
    

    EDIT: changed it to have empty ‘sub’ items where there are no children. See the other variant in edit history.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this php script that output an html table with data about
I'm trying to generate a table of contents from a block of HTML (not
I have the following code to generate a table listing brand/model/submodel/style for a group
I want to generate an HTML table from a couple specified parameters. Specifically, the
this statement will generate a 4 column table: SELECT shipped.badguy AS badguy, shipped.sdate AS
TLDR; How do I read data from a table using Entity Framework, when the
I have a table in MSAccess, which is generated from another software. In that
I have a table of data which is generated dynamically with Javascript. Every few
I use SQLite3 and have a table called blobs that stores content and *hash_value*.
I'd like to add a new column to my data.table, which contains data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.