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

  • Home
  • SEARCH
  • 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 8742339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:29:00+00:00 2026-06-13T11:29:00+00:00

I have experienced some problem by using a nested list in Python in the

  • 0

I have experienced some problem by using a nested list in Python in the code shown bleow.

Basically, I have a 2D list contains all 0 values, I want to update the list value in a loop.

However, Python does not produce the result I want. Is there something that I misunderstand about range() and Python list indices?

some_list = 4 * [(4 * [0])]
for i in range(3):
    for j in range(3):
        some_list[i+1][j+1] = 1
for i in range(4):
    print(some_list[i])

The results I expected are:

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]

But the actual results from Python are:

[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]

What’s going on here?

  • 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-13T11:29:01+00:00Added an answer on June 13, 2026 at 11:29 am

    The problem is caused by the fact that python chooses to pass lists around by reference.

    Normally variables are passed “by value”, so they operate independently:

    >>> a = 1
    >>> b = a
    >>> a = 2
    >>> print b
    1
    

    But since lists might get pretty large, rather than shifting the whole list around memory, Python chooses to just use a reference (‘pointer’ in C terms). If you assign one to another variable, you assign just the reference to it. This means that you can have two variables pointing to the same list in memory:

    >>> a = [1]
    >>> b = a
    >>> a[0] = 2
    >>> print b
    [2]
    

    So, in your first line of code you have 4 * [0]. Now [0] is a pointer to the value 0 in memory, and when you multiply it, you get four pointers to the same place in memory. BUT when you change one of the values then Python knows that the pointer needs to change to point to the new value:

    >>> a = 4 * [0]
    >>> a
    [0, 0, 0, 0]
    >>> [id(v) for v in a]
    [33302480, 33302480, 33302480, 33302480]
    >>> a[0] = 1
    >>> a
    [1, 0, 0, 0]
    

    The problem comes when you multiply this list – you get four copies of the list pointer. Now when you change one of the values in one list, all four change together:

    >>> a[0][0] = 1
    >>> a
    [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
    

    The solution is to avoid the second multiplication. A loop does the job:

    >>> some_list = [(4 * [0]) for _ in range(4)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery 1.6.1 and I have some code that's erroring out in IE7,
Currently I'm using Visual Studio 2008 (SP1) and developing some code that uses nested
I have a problem in a SharePoint list - some fields appear twice on
Using variadic template arguments together with a simple template argument I have experienced some
I experienced some odd behavior while using C++ type traits and have narrowed my
I have experienced some memory managment ambiguitys with opencv. You could do this with
I have experienced Zend Framework 1 and I've build some apps with that framework.
I have some experience using parallel extensions in .Net development, but I was looking
Hey, i have some experience with MVC. but I'm new to rails. I'm using
I've run into some problems debugging a multi-threaded process using GDB. I have a

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.