Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'operator': 0.03; 'initialize': 0.05; 'nested': 0.07; 'received:64.202.165': 0.07; 'python': 0.09; 'agree,': 0.09; 'be:': 0.09; 'replication': 0.09; 'thread,': 0.09; '[none]': 0.16; 'libs': 0.16; 'wrote:': 0.17; 'instance': 0.17; '3.2': 0.22; 'precise': 0.22; 'produces': 0.22; 'subject:skip:i 10': 0.22; 'defined': 0.22; 'references': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'question': 0.27; 'subject:list': 0.28; 'related': 0.30; 'asked': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'list': 0.35; 'so,': 0.35; 'pm,': 0.35; 'there': 0.35; 'list.': 0.35; 'but': 0.36; 'anything': 0.36; "i'll": 0.36; 'beyond': 0.37; 'being': 0.37; 'why': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'think': 0.40; 'easy': 0.60; 'real': 0.61; 'first': 0.61; 'more': 0.63; 'behavior': 0.64; 'here': 0.65; 'watching': 0.65; 'header:Reply-To:1': 0.68; 'answer.': 0.71; 'reply-to:no real name:2**0': 0.72; '"oh,': 0.84; 'nice,': 0.84; 'watched': 0.84; '"that\'s': 0.91; '(running': 0.91; 'was:': 0.91 Date: Sun, 04 Nov 2012 22:44:39 -0800 From: Andrew Robinson User-Agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111126 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Multi-dimensional list initialization References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: andrew3@r3dsolutions.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352097996 news.xs4all.nl 6974 [2001:888:2000:d::a6]:33172 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32751 On 11/04/2012 10:27 PM, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] FYI: The behavior is the same in python 3.2 m=[[None]*4]*4 produces a nested list with all references being to the first instance of the inner list construction. I agree, the result is very counter-intuitive; hmmm... but I think you meant: m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ] rather than: m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] ? :) ? I asked a why question on another thread, and watched several dodges to the main question; I'll be watching to see if you get anything other than "That's the way it's defined in the API". IMHO -- that's not a real answer. My guess is that the original implementation never considered anything beyond a 1d list. :) A more precise related question might be: is there a way to force the replication operator to use copying rather than referencing? :/