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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:51:03+00:00 2026-06-13T04:51:03+00:00

Possible Duplicate: Strange behavior of lists in python I am attempting to initialize a

  • 0

Possible Duplicate:
Strange behavior of lists in python

I am attempting to initialize a 6 by 6 2D array of None. Here is what I do:

arr = [[None]*6]*6

Then I attempt to insert some values as follows:

arr[2][0] = arr[2][1] = 0

Which instead of changing the values of (2,0) and (2,1) to 0 and keeping the rest of the array full of None, it changes all the columns 0 and 1 to 0.

Full example:

>>> arr = [[None]*6]*6
>>> arr
[[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]]

>>> arr[2][0] = arr[2][1] = 0
>>> arr
[[0, 0, None, None, None, None], [0, 0, None, None, None, None], [0, 0, None, None, None, None], [0, 0, None, None, None, None], [0, 0, None, None, None, None], [0, 0, None, None, None, None]]

Why does this happen and how can I prevent it?

p.s. my use of the phrase ‘weird behavior’ implies weird to me, not python messing up 🙂

  • 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-13T04:51:04+00:00Added an answer on June 13, 2026 at 4:51 am

    When you say [None]*6, a list of six None values is created. When you multiply that by 6, it’s six copies of the same list that you get. So modifying one row will also modify all the other rows in the same way.

    You can work around this behaviour by doing this (or see this question for alternatives):

    arr = [[None]*6 for i in xrange(6)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Strange behavior Of foreach Why PHP sometimes change last element of array?
Possible Duplicate: Large numbers erroneously rounded in Javascript I am experiencing some strange behavior
Possible Duplicate: iOS 5.1 with Xcode 4.3.1: [UIColor colorWithPatternImage:] strange behavior only on device
Possible Duplicate: Strange behavior Of foreach Strange behaviour after loop by reference - Is
Possible Duplicate: javascript string exec strange behavior I have a regex and i am
Possible Duplicate: Strange behavior Of foreach Strange behaviour after loop by reference - Is
Possible Duplicate: Mysterious calculation error when multiply by 100 Here is a strange problem
Possible Duplicate: Strange NetworkOnMainThreadException in Android app? Trying To Upload To Dropbox: NetworkOnMainThreadException? I
Possible Duplicate: strange output in comparision of float with float literal Comparison of float
Possible Duplicate: strange output in comparison of float with float literal When I am

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.