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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:38:14+00:00 2026-06-10T17:38:14+00:00

Possible Duplicate: Simple python code about double loop I’m stuck with the well known

  • 0

Possible Duplicate:
Simple python code about double loop

I’m stuck with the well known problem of changing 1 item in a list of lists. I need to use a fixed size list of list.

If I use:

In [21]: a=[[2]*2]*3

In [22]: a
Out[22]: [[2, 2], [2, 2], [2, 2]]

In [23]: a[0][0]=1

In [24]: a
Out[24]: [[1, 2], [1, 2], [1, 2]]

But if I define the list of list in the following way, it works:

In [26]: a = [
   ....: [2,2],
   ....: [2,2],
   ....: [2,2],
   ....: ]

In [27]: a   
Out[27]: [[2, 2], [2, 2], [2, 2]]

In [28]: a[0][0]=1

In [29]: a
Out[29]: [[1, 2], [2, 2], [2, 2]]

To me line 22 and 27 looks identical. So which is the difference?

Can someone explain me how to go around this problem and, in particular, how to change a code that change a single item of a list of list? If this is not possible, any suggestion to move to a different data structure allowing that?
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-10T17:38:15+00:00Added an answer on June 10, 2026 at 5:38 pm

    There is a fundamental difference in the 2 ways you defined the matrix in both code samples. In the first (line 21) when you multiply the outer list by 3 you actually multiply the references to the inner list.

    Here’s a parameterized example:

    a = [2,2]
    b = [a] * 3
    b => [a, a, a] => [[2,2], [2,2], [2,2]]
    

    As you can see the numerical representation of b is what you would expect, but it actually contains 3 references to a. So if you change either of the inner lists, they all change, because they are actually the same list.

    Instead of multiplication you need to clone the lists doing something like this

    new_list = old_list[:]
    

    or

    new_list = list(old_list)
    

    In the case of nested lists, this can be done using loops.

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

Sidebar

Related Questions

Possible Duplicate: Why doesn't this division work in python? A simple problem I'm having
Possible Duplicate: C# Captured Variable In Loop I'm working on a few simple applications
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: Why does simple C code receive segmentation fault? Hey Everyone, I'm sure
Possible Duplicate: Kill Ajax requests using JavaScript using jQuery Here is the simple code
Possible Duplicate: Is it feasible to compile Python to machine code? Is it possible
Possible Duplicate: Why does simple C code receive segmentation fault? I am working in
Possible Duplicate: Short Description of Python Scoping Rules I wrote two simple functions: #
Possible Duplicate: How to hide console window in python? I have a simple script

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.