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

  • Home
  • SEARCH
  • 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 7042571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:11:04+00:00 2026-05-28T02:11:04+00:00

>>> rows = [[”]*5]*5 >>> rows [[”, ”, ”, ”, ”], [”, ”, ”,

  • 0
>>> rows = [['']*5]*5
>>> rows
[['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']]
>>> rows[0][0] = 'x'

Naturally, I expect rows to become:

[['x', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']]

Instead, I get:

[['x', '', '', '', ''], ['x', '', '', '', ''], ['x', '', '', '', ''], ['x', '', '', '', ''], ['x', '', '', '', '']]

It seems that elements of rows list are pointers to the same old [”]*5 list. Why does it work this way and is this a Python feature?

  • 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-28T02:11:05+00:00Added an answer on May 28, 2026 at 2:11 am

    The behaviour is not specific to the repetition operator (*). For example, if you concatenate two lists using +, the behaviour is the same:

    In [1]: a = [[1]]
    
    In [2]: b = a + a
    
    In [3]: b
    Out[3]: [[1], [1]]
    
    In [4]: b[0][0] = 10
    
    In [5]: b
    Out[5]: [[10], [10]]
    

    This has to do with the fact that lists are objects, and objects are stored by reference. When you use * et al, it is the reference that gets repeated, hence the behaviour that you’re seeing.

    The following demonstrates that all elements of rows have the same identity (i.e. memory address in CPython):

    In [6]: rows = [['']*5]*5
    
    In [7]: for row in rows:
       ...:     print id(row)
       ...:     
       ...:     
    15975992
    15975992
    15975992
    15975992
    15975992
    

    The following is equivalent to your example except it creates five distinct lists for the rows:

    rows = [['']*5 for i in range(5)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to get rows from one file, that contains in another file Example, i
I am displaying many rows of data in a list view that is bound
I deleted millions of rows of old data from a production SQL database recently,
I have multiple table rows that has a set of radio buttons on each
I want to delete rows in GlassesColor table that associated with GlassesID in Glasses
tryng to select table rows and then use subqueries to generate single row list
I'm inserting rows in a <table> that are bound to a JSON object. A
I have 30,000 rows in a database that need to be similarity checked (using
I would like to rows that have only been entered in the last 1
Scenario: List containing 'rows' of list records with a variety of values Problem: List.sort

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.