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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:31:55+00:00 2026-05-26T00:31:55+00:00

Why? Look they are equal, initially. You do the same operation but hey you

  • 0

Why? Look they are equal, initially. You do the same operation but hey you get different structures. Why do the two methods of constructing nested lists give different results?

>>> lll=[[[[[]]*2 for a in xrange(2)] for b in xrange(2)]]
>>> ll=[[[[[]]*2]*2]*2]
>>> lll
[[[[[], []], [[], []]], [[[], []], [[], []]]]]
>>> ll
[[[[[], []], [[], []]], [[[], []], [[], []]]]]
>>> ll==lll
True
>>> ll[0][0][0][0]=1
>>> lll[0][0][0][0]=1
>>> ll==lll
False           #Why?

Structure

[
        [
                [
                        [
                                [], 
                                []
                        ],
                        [
                                [], 
                                []
                        ]
                ],
                [
                        [
                                [], 
                                []
                        ],
                        [
                                [], 
                                []
                        ]
                ]
        ]
]
  • 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-26T00:31:56+00:00Added an answer on May 26, 2026 at 12:31 am

    When you use list comprehensions like that, new lists are created for each iteration.

    When you multiply a list, the same reference is duplicated over and over.

    When that reference is to a list, it means that the same inner list is duplicated over and over.

    So, when you multiply, there is only one empty list, referenced twice by a single list, which is referenced twice by a single list, which is referenced twice by another single list, which is the only item in the final, outer list.

    When you use a list comprehension, each reference is to a new, different list.

    So, when you point the first item of the list containing two references to the empty list to a different object, you see the change everywhere that list is referenced. Since it’s referenced twice by a list that is itself referenced twice, you see the change in four places.

    If you were to append a value to the very inner list:

    ll[0][0][0][0].append(1)
    

    You’d see that value referenced eight times:

    [[[[[1], [1]], [[1], [1]]], [[[1], [1]], [[1], [1]]]]]
    

    because all the inner lists are actually the same list.

    The real structure of ll isn’t what it appears to be above, but:

    [                   ]
             |
     [       ,         ]
         \       /
         [   ,   ]
           \   /
           [ , ]
            \ /
            []
    

    The way it is displayed by Python is quite misleading.

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

Sidebar

Related Questions

These two may look like they have no correlation but bear with me! In
I have some C structures related to a 'list' data structure. They look like
Same for methods too: I am given two instances of PropertyInfo or methods which
I keep coming across (rare) cases where NSStrings don't return equal although they look
how do I create a list of li's (they look like simple boxes by
alt text http://imgkk.com/i/Fmtd8B.jpg This is how they look in vista. They are glossier than
I've read the description of functionoids here . They look like a poor-man's version
I have a page that has 3 variables. They look like this: String[] Headers
I have a set of strings with numbers embedded in them. They look something
I'm using ggplot2 to make some bullseye charts in R. They look delightful, and

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.