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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:29:45+00:00 2026-06-04T22:29:45+00:00

Very simple code nested example: All the code does is create a list of

  • 0

Very simple code nested example:

All the code does is create a list of lists that is initialized to zero.
It iterates through the list rows and columns and each position is given a value.
For some reason the last row of the 2D list is duplicated for each row when the final vector is printed.

Number_of_channels=2
Coefficients_per_channel=3

coefficient_array=[[0]*Coefficients_per_channel]*Number_of_channels 
print coefficient_array

for channel in range(Number_of_channels):
    for coeff in range(Coefficients_per_channel):
        coefficient_array[channel][coeff]=coeff*channel
        print coefficient_array[channel][coeff]

print coefficient_array

Output:

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

I actually expect:

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

Anyone have any idea how come this is happening?

  • 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-04T22:29:47+00:00Added an answer on June 4, 2026 at 10:29 pm

    You only duplicate the outer list, but the values of that list are left untouched. Thus, all (both) outer lists contain references to the same inner, mutable list.

    >>> example = [[1, 2, 3]]
    >>> example *= 2
    >>> example
    [[1, 2, 3], [1, 2, 3]]
    >>> example[0][0] = 5
    [[5, 2, 3], [5, 2, 3]]
    >>> example[0] is example[1]
    True
    

    Better create the inner lists in a loop:

    coefficient_array=[[0]*Coefficients_per_channel for i in xrange(Number_of_channels)]
    

    or, illustrated with the python prompt again:

    >>> example = [[i, i, i] for i in xrange(2)]
    >>> example
    [[0, 0, 0], [1, 1, 1]]
    >>> example[0][0] = 5
    >>> example
    [[5, 0, 0], [1, 1, 1]]
    >>> example[0] is example[1]
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this very simple code example, messages get lost every once in a while.
Previously I've written some very simple multithreaded code, and I've always been aware that
I have created a very simple nested loop example and am struggling to write
I have very simple code: List<String> list = new ArrayList<String>(); String a = a;
I've written some very simple code (a Google Chrome extension) that gets the keywords
I have a very simple bit of code that won't work, and have no
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have a very simple code: package mygame; public class RunGame { public static
I have some VERY simple code to return the title for a section header:
I have a very simple code, which annoyingly was working and for the life

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.