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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:19:29+00:00 2026-06-06T13:19:29+00:00

Here is a very simple program: a = [[]]*3 print str(a) a[0].append(1) a[1].append(2) a[2].append(3)

  • 0

Here is a very simple program:

 a = [[]]*3

 print str(a)

 a[0].append(1)
 a[1].append(2)
 a[2].append(3)

 print str(a[0])
 print str(a[1])
 print str(a[2])

Here is the output I was expecting:

 [[], [], []]
 [1]
 [2]
 [3]

But instead I get this :

 [[], [], []]
 [1, 2, 3]
 [1, 2, 3]
 [1, 2, 3]

There is really something I do not get here !

  • 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-06T13:19:31+00:00Added an answer on June 6, 2026 at 1:19 pm

    You must do

    a = [[] for i in xrange(3)]
    

    not

    a = [[]]*3
    

    Now it works:

    $ cat /tmp/3.py
    a = [[] for i in xrange(3)]
    
    print str(a)
    
    a[0].append(1)
    a[1].append(2)
    a[2].append(3)
    
    print str(a[0])
    print str(a[1])
    print str(a[2])
    
    $ python /tmp/3.py
    [[], [], []]
    [1]
    [2]
    [3]
    

    When you do something like a = [[]]*3 you get the same list [] three times in a list. The same means that when you change one of them you change all of them (because there is only one list that is referenced three times).

    You need to create three independent lists to circumvent this problem. And you do that with a list comprehension. You construct here a list that consists of independent empty lists []. New empty list will be created for each iteration over xrange(3) (the difference between range and xrange is not so important in this case; but xrange is a little bit better because it does not produce the full list of numbers and just returns an iterator object instead).

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

Sidebar

Related Questions

I guess there will be a very simple answer to this. But here goes.
I'm pretty sure there is a very simple solution here but I can't seem
Here's a very simple java program to print the first line of a file:
Here's a very simple program using the function: #include <windows.h> #include <tchar.h> #include <atlstr.h>
Here's some very simple sample HTML for purposes of this question: <!DOCTYPE html> <html>
I am probably missing something very simple, but here is the situation: I have
I have a very simple print program called print.c: #include <stdio.h> int main(void){ printf(Random
I have no idea what is wrong! This is a very simple program and
I'm trying to write a simple program using WinSnmp in C++. There is very
I have a very odd issue trying to run this quite simple C program

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.