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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:05+00:00 2026-06-05T06:44:05+00:00

I have a list with nested lists and I want to create a function

  • 0

I have a list with nested lists and I want to create a function (modify_list) that gets a tuple and modifies the passed pointer with a passed value argument. The problem is that I’m not sure how to modify a nested value like this programmatically by reference.

Simplified example:

l = [[1, [2,3, [4,5,6]]]]

If I call the function modify_list, these would be how to use it and the expected results:

> l[0][1][2][2]
6

> modify_list((0, 1, 2, 2), 8)
> l
[[1, [2,3, [4,5,8]]]]

> modify_list((0, 1, 1), 14)
> l
[[1, [2,14, [4,5,8]]]]

Thanks

  • 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-05T06:44:07+00:00Added an answer on June 5, 2026 at 6:44 am

    You can determine each sublist by accessing it with the respective index. Use the last index to assign the value:

    def set_nested_val(l, indices, val):
      for i in indices[:-1]:
        l = l[i]
      l[indices[-1]] = val
    

    Note that this function operates on an arbitrary list (the first argument), and not only l. If you want to always modify l, use functools.partial:

    import functools
    l = [[1, [2,3, [4,5,6]]]]
    modify_list = functools.partial(set_nested_val, l)
    

    Note that nested lists, and accessing values by indicies, are often a sign of a bad data architecture. Have you considered a dict whose keys are tuples?

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

Sidebar

Related Questions

How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I have a nested unsorted list and I want to place a click event
I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have Python nested list that I'm trying to organize and eventually count number
Suppose you have a list of 100 complex nested object, and you want to
I have a list of business objects that I want to display in a
I have an arraylist of objects that I want to create all possible combinations
I have a sign-up form that has nested associations/attributes whatever you want to call
I have a List object, with nested Tasks. I have created a page that
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',

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.