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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:49:19+00:00 2026-05-10T18:49:19+00:00

I created a list of lists: >>> xs = [[1] * 4] * 3

  • 0

I created a list of lists:

>>> xs = [[1] * 4] * 3 >>> print(xs) [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] 

Then, I changed one of the innermost values:

>>> xs[0][0] = 5 >>> print(xs) [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] 

Why did every first element of each sublist change to 5?


See also:

  • How do I clone a list so that it doesn't change unexpectedly after assignment? for workarounds for the problem

  • List of dictionary stores only last appended value in every iteration for an analogous problem with a list of dicts

  • How do I initialize a dictionary of empty lists in Python? for an analogous problem with a dict of lists

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

    When you write [x]*3 you get, essentially, the list [x, x, x]. That is, a list with 3 references to the same x. When you then modify this single x it is visible via all three references to it:

    x = [1] * 4 xs = [x] * 3 print(f"id(x): {id(x)}") # id(x): 140560897920048 print(     f"id(xs[0]): {id(xs[0])}\n"     f"id(xs[1]): {id(xs[1])}\n"     f"id(xs[2]): {id(xs[2])}" ) # id(xs[0]): 140560897920048 # id(xs[1]): 140560897920048 # id(xs[2]): 140560897920048  x[0] = 42 print(f"x: {x}") # x: [42, 1, 1, 1] print(f"xs: {xs}") # xs: [[42, 1, 1, 1], [42, 1, 1, 1], [42, 1, 1, 1]] 

    To fix it, you need to make sure that you create a new list at each position. One way to do it is

    [[1]*4 for _ in range(3)] 

    which will reevaluate [1]*4 each time instead of evaluating it once and making 3 references to 1 list.


    You might wonder why * can’t make independent objects the way the list comprehension does. That’s because the multiplication operator * operates on objects, without seeing expressions. When you use * to multiply [[1] * 4] by 3, * only sees the 1-element list [[1] * 4] evaluates to, not the [[1] * 4 expression text. * has no idea how to make copies of that element, no idea how to reevaluate [[1] * 4], and no idea you even want copies, and in general, there might not even be a way to copy the element.

    The only option * has is to make new references to the existing sublist instead of trying to make new sublists. Anything else would be inconsistent or require major redesigning of fundamental language design decisions.

    In contrast, a list comprehension reevaluates the element expression on every iteration. [[1] * 4 for n in range(3)] reevaluates [1] * 4 every time for the same reason [x**2 for x in range(3)] reevaluates x**2 every time. Every evaluation of [1] * 4 generates a new list, so the list comprehension does what you wanted.

    Incidentally, [1] * 4 also doesn’t copy the elements of [1], but that doesn’t matter, since integers are immutable. You can’t do something like 1.value = 2 and turn a 1 into a 2.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Everything's possible :) EDIT: haste never leads to quality nor… May 11, 2026 at 12:33 pm
  • added an answer With Django 1.1, which is currently in beta, I would… May 11, 2026 at 12:33 pm
  • added an answer The format for generics is the name, a ` character,… May 11, 2026 at 12:33 pm

Related Questions

If I create a recursive list of of lists: class myList { List<myList> childLists;
If I have List<String> text how can I create a sub-list of all continious
I have a field object and I create a list of fields: class Field
I want to create a list of columns in SQL Server 2005 that have
.NET offers a generic list container whose performance is almost identical (see Performance of
I am developing, a simple SharePoint Sequential Workflow which should be bound to a
I'm trying to show someone a use for interfaces in a crazy situation they've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.