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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:07:20+00:00 2026-06-04T15:07:20+00:00

I don’t understand why the following code behaves the way it does. import numpy

  • 0

I don’t understand why the following code behaves the way it does.

import numpy as np


nbr_arrays = 4
nbr_fields_per_array = 3
nbr_subfields_per_field = 2

# pre-allocate zeros list
zeros = np.zeros(nbr_subfields_per_field)

data = []
for array in range(nbr_arrays):

    # pre-allocate the subarray 
    empty_array = []
    for empty_array_index in range(nbr_fields_per_array):
        empty_array.append(zeros)

    # append pre subarray to data
    data.append(empty_array)

    # fill up data
    for j in range(nbr_fields_per_array):
        for k in range(nbr_subfields_per_field):
            data[array][j][k] = j*k*array

The generated output data reads now:

[[array([ 0.,  6.]), array([ 0.,  6.]), array([ 0.,  6.])],
 [array([ 0.,  6.]), array([ 0.,  6.]), array([ 0.,  6.])],
 [array([ 0.,  6.]), array([ 0.,  6.]), array([ 0.,  6.])],
 [array([ 0.,  6.]), array([ 0.,  6.]), array([ 0.,  6.])]]

Even zeros reads completely differently:

array([ 0.,  6.])

If I look at the identify of the different lists, this is what I get:

id(data[0][0])
Out[72]: 45790208

id(data[1][0])
Out[66]: 45790208

id(data[2][0])
Out[67]: 45790208

id(data[3][0])
Out[68]: 45790208

id(zeros)
Out[69]: 45790208

why are all the references the same? and why does zero suddenly contain non-zero values?

I’d really appreciate it if somebody could explain me what exactly is happening here, and how I have to modify my code to see the expected behaviour (output).

EDIT:

not using zeros but using [[0]*nbr_subfields_per_field for x in range(nbr_fields_per_array)] instead gives me the expected result. but why? why doesn’t the original code work?
Modified code that works:

data = []
for array in range(nbr_arrays):
    empty_array = [[0]*nbr_subfields_per_field for x in range(nbr_fields_per_array)]

    ''' this is causing the weird behaviour 
    empty_array = []
    for empty_array_index in range(nbr_fields_per_array):
        empty_array.append(zeros)
    '''

    data.append(empty_array)

    for j in range(nbr_fields_per_array):
        for k in range(nbr_subfields_per_field):
            data[array][j][k] = j*k*array
  • 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-04T15:07:22+00:00Added an answer on June 4, 2026 at 3:07 pm
    # pre-allocate zeros list
    zeros = np.zeros(nbr_subfields_per_field)
    

    This creates a single object.

    for empty_array_index in range(nbr_fields_per_array):
        empty_array.append(zeros)
    

    This keeps appending the same object.

    Stop pre-allocating.

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

Sidebar

Related Questions

I don't understand the meaning of the keyword static when I import System class:
Don't be scared of the extensive code. The problem is general. I just provided
Don't know how to google for such, but is there a way to query
Don't ask me why but I need to do the following: string ClassName =
I don't want to know a way to preload images, I found much on
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
Don't know a whole lot about streams. Why does the first version work using
Don't ask why, but is there any way to suppress a failed linking error?

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.