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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:10:35+00:00 2026-06-05T09:10:35+00:00

I just met something really strange of Python: >>> out=[[0]*3]*3 >>> out [[0, 0,

  • 0

I just met something really strange of Python:

>>> out=[[0]*3]*3
>>> out
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> out[0][1]
0
>>> out[0][1]=9
>>> out
[[0, 9, 0], [0, 9, 0], [0, 9, 0]]

well, obviously, what I want is :

[[0, 9, 0], [0, 0, 0], [0, 0, 0]]

isn’t strange? I’m not very familiar with Python, but Python always impresses me with its intuitive behavior. But how it comes up with this?
… and how can I get what I need?

thanks!

Watt

  • 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-05T09:10:37+00:00Added an answer on June 5, 2026 at 9:10 am

    A strange behaviour indeed, but that’s only because * operator makes shallow copies, in your case – shallow copies of [0, 0, 0] list. You can use the id() function to make sure that these internal lists are actually the same:

    out=[[0]*3]*3
    id(out[0])
    >>> 140503648365240
    id(out[1])
    >>> 140503648365240
    id(out[2])
    >>> 140503648365240
    

    Comprehensions can be used to create different lists as follows:

    out = [ [0]*3 for _ in range(3) ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm applying UnitTest just for a while, and today I met something very strange.
I met a very strange problem recently. I want to control the ListBoxItem's render
I just met a strange behavior of facet_grid() in ggplot2 0.9, and I wonder
This is a very simple issue. I must just be doing something stupid: This
Just wondering how I can make my app open automatically at login, but make
Just wondering if the following is considered to be good programming practice or not?
Just looking for a good PHP Image Library, I want to display images with
Just out of curiosity, how does iostream access the input-output system. (I have a
Just wanted to know if anyone is really using Objects and Collections in Oracle
We met a strange situation on SQL Server 2008 (SP1) - 10.0.2531.0 (X64) -

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.