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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:20:22+00:00 2026-05-15T09:20:22+00:00

I expected to get a grid of unique random numbers. Instead each row is

  • 0

I expected to get a grid of unique random numbers. Instead each row is the same sequence of numbers. What’s going on here?

from pprint import pprint
from random import random

nrows, ncols = 5, 5
grid = [[0] * ncols] * nrows
for r in range(nrows):
    for c in range(ncols):
        grid[r][c] = int(random() * 100)
pprint(grid)

Example output:

[[64, 82, 90, 69, 36],
 [64, 82, 90, 69, 36],
 [64, 82, 90, 69, 36],
 [64, 82, 90, 69, 36],
 [64, 82, 90, 69, 36]]
  • 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-05-15T09:20:23+00:00Added an answer on May 15, 2026 at 9:20 am

    I think that this is because python uses a weak copy of the list when you call

    grid = [...] * nrows
    

    I tried hard coding the list and it worked correctly:

    >>> grid = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
    >>> for r in range(nrows):
    ...     for c in range(ncols):
    ...             grid[r][c] = int(random() * 100)
    ... 
    >>> pprint(grid)
    [[67, 40, 41, 50, 92],
     [26, 42, 64, 77, 77],
     [65, 67, 88, 77, 76],
     [36, 21, 41, 29, 25],
     [98, 77, 38, 40, 96]]
    

    This tells me that when python copies the list 5 times, all it is doing is storing 5 pointers to your first list – then, when you change the values in that list, you are actually just changing the value in the first list and it gets reflected in all lists which point to that one.

    Using your method, you can’t update all the list independently.

    Instead, I would suggest changing your list generation line to look more like this:

    grid = [[0] * ncols for row in range(ncols)]
    

    That should create 5 independent lists for you.

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

Sidebar

Related Questions

We get expected : end of statement error for the below code and VB
I get the Operator Expected error at the end of the two following rules
i get this error when trying this: ERROR method name expected. How should i
Expected output & output I get in debug mode, and release mode under VS2010,
Doing an ajax get request works as expected using the following code: $.ajax({ type:
I get the following error: ActiveRecord::AssociationTypeMismatch in ContractsController#create ExchangeRate(#2183081860) expected, got HashWithIndifferentAccess(#2159586480) Params: {commit=>Create,
I get the following error with the code below. expected constructor, destructor, or type
When I run the following code I get B as expected: class A {
While trying to validate my forms i get the following error: Expected a {
-(NSMutableArray *) forwardInterfaceList: (networkInterface *) i; I get the error Expected ')' before 'networkInterface'

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.