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 9133705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:32:02+00:00 2026-06-17T08:32:02+00:00

How do I insert an element into a program-specified level of a list? My

  • 0

How do I insert an element into a program-specified level of a list? My solution isn’t very Pythonic:

def listInsertDepth(l,e,i,lvl): # Insert element e into list l at depth lvl using list of indices i
    if lvl < 0: # That is, if your depth level is invalid
        return l
    else:
        assert len(i) == lvl+1 # One index for every level, plus for the actual insertion
        s = l # A copy for tampering with
        for index in range(lvl):
            s = s[i[index]]
        s.insert(i[-1],e)
        return listInsertDepth(l,s,i[:-1],lvl-1) 
  • 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-06-17T08:32:03+00:00Added an answer on June 17, 2026 at 8:32 am

    Given a series of indices, you can simply loop over them all except for the last one to traverse your nested structure to the parent list to insert into:

    listInsertAtDepth(lst, value, indices):
        parent = lst
        for index in indices[:-1]:
            parent = parent[index]
        parent.insert(indices[-1], value)
    

    You can add a try, except combo to detect that you got your index wrong:

    listInsertAtDepth(lst, value, indices):
        parent = lst
        try:
            for index in indices[:-1]:
                parent = parent[index]
            parent.insert(indices[-1], value)
         except IndexError:
            return None
    

    but personally, I’d rather get an exception than having it swallowed and discarded like that.

    Note that you should not return lst from your function, as it is altered in-place. Python stdlib methods like .append() and .extend(), that modify a list in-place don’t return anything either.

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

Sidebar

Related Questions

I'm trying to write a program in Prolog, which will insert an element into
Is there a pythonic way to insert an element into every 2nd element in
I need to dynamically insert an element into an existing list of blog posts.
What's the best way to insert an element into a page's HEAD directly before
I found a strange thing. When I insert an element into another with the
Consider an initially empty RB-tree, which we insert m elements into. Inserting an element
I have a std::deque, and I want to insert an element at a specified
I wrote a C++ program with the goal of quickly inserting an element into
I am using insertRow to insert a TR element into a table, but for
I want to insert new element into a vector using a pointer I have

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.