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

The Archive Base Latest Questions

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

I was writing a program that needs a 2d array, and came upon a

  • 0

I was writing a program that needs a 2d array, and came upon a strange problem.

At first, I wrote:

board = [[]]*11

to make eleven arrays within an array.

Then I wanted eleven blanks within each array so I wrote:

for i in range(11):
    board[i].append(' ')

I wanted to fill the third array, from index 4 to 7 with stars, so I wrote:

for i in range(4, 7):
    board[2][i] = '*'

But then unexpectedly, instead of only the third array being affected by the code, all of the arrays in the “board” were changed.

So of course, I wrote:

board2 = []
for i in range(11):
    board2.append([])

to create my array, and then wrote:

for i in range(11):
    for i in range(11):
        board2[i].append(' ')

to create my array with 11 arrays containing 11 blanks.

After I created my array like that, I was able to run the code to add the stars, and as expected, only affected one array instead of all of them.

Could anybody please explain what are the difference between the two seemingly similar arrays?

  • 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-20T18:53:16+00:00Added an answer on May 20, 2026 at 6:53 pm

    The list [[]] * 11 contains 11 references to the same list.

    Your second example creates 11 different lists.

    board = [[]]*11
    #        ^^ this is called just once
    
    board2 = []
    for i in range(11):
        board2.append([])
    #                 ^^ this is called 11 times, creating 11 different lists
    

    Another way to write the second example is to use a list comprehension:

    [[] for i in range(11)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that needs to take an array of size n
I am writing a program that needs to run a java.jar server. I need
I'm writing a program that needs to print off high resolution .tiff images. The
I'm writing a program that needs to access a file inside of c:\program files\program_name\
I writing a JSP program that needs to react on an existing program. It
I am writing a java program that needs a file open dialog. The file
I'm writing a C program that needs good error handling. The code likes like
OK, so I am writing a program that unfortunately needs to use a huge
I am writing an MPI program that needs to read a part of a
I'm writing a C# program that needs to call MATLAB processing routines. I've been

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.