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

Related Questions

I created a linked list and when I tried to print values of the
I created a list with a TextView and an EditText in each item. In
I have dynamically created some list of RadioButtons with some values. pro grammatically I
I have a scenario where I'm pushing change-lists to another system. Each list contains
in my python code i want to create a list of lists...in a loop
This might be basic question but how do I create a list of lists
I am looking for a convenient way to create a list of lists for
I have a dynamically created list. Items from the list should be able to
I am trying to use dynamically created list in WHERE, for example: select *
I created the list that have device detected, and I want to send data

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.