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

The Archive Base Latest Questions

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

I have a list of lists called hab acting as a 2D array. In

  • 0

I have a list of lists called hab acting as a 2D array. In this list of lists I am storing elements of a class called loc which is why I am not using a numpy array (it’s not storing numbers).

I want to fill each element with a randomly picked ‘loc’ by looping through each element. However it seems that whenever I get to the end of a row, the program takes the final row element and puts it in all the other elements in that row. This means that I end up with the list of lists looking like this:

3 3 3 3 3  
1 1 1 1 1  
2 2 2 2 2  
2 2 2 2 2  
4 4 4 4 4 

when actually I want all these numbers to be random (this is printing out a specific trait of each loc which is why it is numbers).

Here is the relevant bit of code:

allspec=[] # a list of species
for i in range(0,initialspec):
    allspec.append(species(i)) # make a new species with new index
    print 'index is',allspec[i].ind, 'pref is', allspec[i].pref
hab=[[0]*xaxis]*yaxis
respect = randint(0,len(allspec)-1)
for j in range(0,yaxis):
    for k in range (0,xaxis):
        respect=randint(0,len(allspec)-1)
        print 'new species added at ',k,j,' is ', allspec[respect].ind
        hab[k][j]=loc(k,j,random.random(),allspec[respect])
        print 'to confirm, this is ', hab[k][j].spec.ind

    for k in range (0,xaxis):
        print hab[k][j].spec.ind

printgrid(hab,xaxis,yaxis)
print 'element at 1,1', hab[1][1].spec.ind

Within the loop, I am confirming that the element I have created is what I want it to be with the line print 'to confirm, this is ', hab[k][j].spec.ind and it is fine at this point. It is only when that loop is exited that it somehow fills every element on the row with the same thing. I don’t understand!

  • 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-15T17:31:39+00:00Added an answer on June 15, 2026 at 5:31 pm

    The problem is here:

    hab=[[0]*xaxis]*yaxis
    

    As a result of the above statement, hab consists of yaxis references to the same list:

    In [6]: map(id, hab)
    Out[6]: [18662824, 18662824, 18662824]
    

    When you modify hab[k][j], all other hab[][j] change too:

    In [10]: hab
    Out[10]: [[0, 0], [0, 0], [0, 0]]
    
    In [11]: hab[0][0] = 42
    
    In [12]: hab
    Out[12]: [[42, 0], [42, 0], [42, 0]]
    

    To fix, use

    hab=[[0]*xaxis for _ in range(yaxis)]
    

    Now each entry of hab refers to a separate list:

    In [8]: map(id, hab)
    Out[8]: [18883528, 18882888, 18883448]
    
    In [14]: hab
    Out[14]: [[0, 0], [0, 0], [0, 0]]
    
    In [15]: hab[0][0] = 42
    
    In [16]: hab
    Out[16]: [[42, 0], [0, 0], [0, 0]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a list somewhere called majorPowers which contain these two lists:
I have a list of objects called Activity: class Activity { public Date activityDate;
If I have a list of objects called Car: public class Car { public
well i need to make a class called TList which have a private attribute
I'm building an asp.net MVC 2 app. I have a list view which lists
Say I have a linked list called L that stores linked lists so: LinkedList
I think its called a slice... basically I have a list of lists: [['fmt/10',
I have a list of drop down lists populated form a database(using ASP.NET). These
I have defined an S4 class called cell , instances of which I would
I have a model List with an attribute called description of type string. Not

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.