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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:39:17+00:00 2026-05-27T16:39:17+00:00

OK, check out the following codes first: Demo1 = [[], []] Demo2 = [[]]

  • 0

OK, check out the following codes first:

Demo1 = [[], []]
Demo2 = [[]] * 2
Demo1[0].append(1)
Demo2[0].append(1)
print "Demo1: ", Demo1
print "Demo2: ", Demo2

And here’s the output:

Demo1:  [[1], []]
Demo2:  [[1], [1]]

I need to create a list whose items are all list as well just like Demo1 and Demo2, of course I used Demo2 in my script and it kept getting into trouble until I found the reason which is what you can see from above codes. So why is this happening? Most of the cases I would use Demo2 to create such list as its length differs each time, but how do I append an item to separate lists within the list without getting into such mess?

  • 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-27T16:39:18+00:00Added an answer on May 27, 2026 at 4:39 pm

    For you first question: It is happening because in Demo2 case your list contains two copies of the same object. See for example below where I print the memory locations of those elements, noting that they differ for Demo1 but match for Demo2.

    >>> Demo1 = [[], []]
    >>> Demo2 = [[]] * 2
    >>> print id(Demo1[0]), id(Demo1[1])
    33980568 34018800
    >>> print id(Demo2[0]), id(Demo2[1])
    34169920 34169920
    

    For your second question: you could use a list comprehension like [[] for i in xrange(n)], in order to be creating a new list n times rather than duplicating the same list n times.

    Example:

    >>> Demo2 = [[] for i in xrange(2)]
    >>> Demo2
    [[], []]
    >>> Demo2[0].append(1)
    >>> Demo2
    [[1], []]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please check out the following func and its output void main() { Distance d1;
First check out this code. I seems like it should work for me, but
Can check out here alt text http://51hired.com/static/problem.bmp
I use the following piece of code to check out and then check in
I have the following code //check out the tags, if it allready exist update
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Please check out http://rainsongmanchesters.net/news.htm and notice the black strip above the header image. Now
Please check out: http://gherkin.co.nz/refresh/ and click on the purple box that says 'Membership chest'
when check out done in documentum. how to remove values of Signed Out To

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.