beginner question about range with list and list

Discussion in 'Python' started by erik gartz, Nov 24, 2006.

  1. erik gartz

    erik gartz Guest

    Hello,
    I'm new to python and I'm having difficulty understanding the following
    code. Why doesn't the variable a contain [[{}, {'x': 0}, {}], [{},
    {'x': 1}, {}]] instead. Doesn't {} allocate new memory for the
    dictionary each time? It almost appears as if the 2nd dictionary
    created overwrites the first one. Thanks for your help,
    Erik

    >>>
    >>> a = [[{}] * 3] * 2
    >>> a

    [[{}, {}, {}], [{}, {}, {}]]
    >>> for i in range(2):

    a[1] = {'x':i}
    >>> a

    [[{}, {'x': 1}, {}], [{}, {'x': 1}, {}]]
    >>>
    erik gartz, Nov 24, 2006
    #1
    1. Advertising

  2. erik gartz

    Ben Finney Guest

    "erik gartz" <> writes:

    > Doesn't {} allocate new memory for the dictionary each time? It
    > almost appears as if the 2nd dictionary created overwrites the first
    > one.


    <URL:http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm>

    --
    \ "There are only two ways to live your life. One is as though |
    `\ nothing is a miracle. The other is as if everything is." -- |
    _o__) Albert Einstein |
    Ben Finney
    Ben Finney, Nov 24, 2006
    #2
    1. Advertising

  3. erik gartz

    Dan Lenski Guest

    erik gartz wrote:
    > Doesn't {} allocate new memory for the
    > dictionary each time? It almost appears as if the 2nd dictionary
    > created overwrites the first one. Thanks for your help,
    > Erik
    >
    > >>>
    > >>> a = [[{}] * 3] * 2
    > >>> a

    > [[{}, {}, {}], [{}, {}, {}]]
    > >>> for i in range(2):

    > a[1] = {'x':i}
    > >>> a

    > [[{}, {'x': 1}, {}], [{}, {'x': 1}, {}]]
    > >>>


    You're right about both parts... sort of :)

    Each time that {} is EVALUATED, it allocates a brand new dictionary.
    However you're using the list repetition operator (* n). This operator
    evaluates the list ONLY ONCE and then repeats that list n times.

    The expression [{}]*3 will generate a list that contains the SAME
    dictionary 3 times in a row. The expression [[{}]*3]*2 will generate a
    list that contains the SAME list twice in a row. Thus, when you assign
    to a[1], you are modifying a[0][1] and a[1][1], since a[0] and a[1]
    are the same list.

    The expression {'x':i} does indeed create a new dictionary each time it
    is evaluated... however it gets assigned to the SAME list element each
    time.

    This is a fairly subtle point... if you want to make a new COPY of a
    list (or other mutable object) rather than simply a new reference to
    eat, you say a=b.copy() rather than just a=b. The ability to have
    multiple references to one object is general considered a feature in
    most programming languages, once you get used to it!

    If you want to generate a 2x3 array with each element a DISTINCT
    dictionary belonging to a DISTINCT list, try this:

    a = [[{} for j in range(3)] for i in range(2)]
    for i in range(2):
    for j in range(3):
    a[j] = {'row': i, 'column': j}

    Dan
    Dan Lenski, Nov 24, 2006
    #3
  4. erik gartz wrote:

    > I'm new to python and I'm having difficulty understanding the following
    > code. Why doesn't the variable a contain [[{}, {'x': 0}, {}], [{},
    > {'x': 1}, {}]] instead. Doesn't {} allocate new memory for the
    > dictionary each time?


    each time it's *executed*, yes. [{}]*3 doesn't execute [] or {} three
    times; it executes them both once, and then creates a new list with
    three references to the dictionary. see:

    http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm

    </F>
    Fredrik Lundh, Nov 24, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    571
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Replies:
    46
    Views:
    966
    Antoon Pardon
    Jul 25, 2006
  3. Lambda
    Replies:
    2
    Views:
    392
    James Kanze
    Jul 16, 2008
  4. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    148
    Tomoyuki Kosimizu
    Nov 27, 2003
  5. David Bird
    Replies:
    1
    Views:
    208
    Tiago Macedo
    Jun 23, 2008
Loading...

Share This Page