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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:22:02+00:00 2026-06-07T14:22:02+00:00

Running the code listoflists = [] list = [] for i in range(0,10): list.append(i)

  • 0

Running the code

listoflists = []
list = []
for i in range(0,10):
    list.append(i)
    if len(list)>3:
        list.remove(list[0])
        listoflists.append((list, list[0]))
print listoflists

returns

[([7, 8, 9], 0), ([7, 8, 9], 0), ([7, 8, 9], 0), ([7, 8, 9], 1), ([7, 8, 9], 2), ([7, 8, 9], 3), ([7, 8, 9], 4), ([7, 8, 9], 5), ([7, 8, 9], 6), ([7, 8, 9], 7)]

so somehow the first argument of each tuple (list) is being updated each time in the list of lists, but the second argument list[0] is not. Can someone explain what’s going on here and suggest a way to fix this? I’d like to output

[([0],0), ([0,1],0), ...
  • 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-07T14:22:04+00:00Added an answer on June 7, 2026 at 2:22 pm

    Lists are a mutable type – in order to create a copy (rather than just passing the same list around), you need to do so explicitly:

    listoflists.append((list[:], list[0]))
    

    However, list is already the name of a Python built-in – it’d be better not to use that name for your variable. Here’s a version that doesn’t use list as a variable name, and makes a copy:

    listoflists = []
    a_list = []
    for i in range(0,10):
        a_list.append(i)
        if len(a_list)>3:
            a_list.remove(a_list[0])
            listoflists.append((list(a_list), a_list[0]))
    print listoflists
    

    Note that I demonstrated two different ways to make a copy of a list above: [:] and list().

    The first, [:], is creating a slice (normally often used for getting just part of a list), which happens to contain the entire list, and thus is effectively a copy of the list.

    The second, list(), is using the actual list type constructor to create a new list which has contents equal to the first list. (I didn’t use it in the first example because you were overwriting that name in your code – which is a good example of why you don’t want to do that!)

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

Sidebar

Related Questions

when running code like this: use strict; print Dumper something; nothing is printed out
VB.NET 2010, .NET 4 Hello, I tried running code analysis the first time and
After running code from my previous question , I get a result set from
While running code in production, I don't have to interact with the Ember.js runloop
I am running code to produce outputs where I want all the outputs to
Hello friends i am running code given below which contains the setLogTimeEntery function and
I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
What is the best way to ignore running code when a button is clicked?
With Clojure (and other Lisp dialects) you can modify running code. So, when a
I have a thread in my application that is running code that can potentially

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.