Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.064 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'operator': 0.03; 'initialize': 0.05; 'cc:addr:python-list': 0.10; '[none]': 0.16; 'backward': 0.16; 'cc:name:python list': 0.16; 'gotcha.': 0.16; 'instantiate': 0.16; 'libs': 0.16; 'wrote:': 0.17; 'subject:skip:i 10': 0.22; 'cc:2**0': 0.23; 'elements': 0.23; "i've": 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'implemented': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'behaviour': 0.29; 'received:209.85.215.46': 0.30; 'anyone': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'so,': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'but': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'subject:-': 0.40; 'header:Received:5': 0.40; 'easy': 0.60; 'relatively': 0.62; 'between': 0.63; 'different': 0.63; 'ever': 0.63; 'times': 0.63; 'within': 0.64; 'here': 0.65; '"oh,': 0.84; 'nice,': 0.84; 'oscar': 0.84; '(running': 0.91; 'was:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=6v+yKde/eQuWjP05MTSxBzGX3c648n0rnotZVntw/yQ=; b=ZdBnMFxI8nV9ftUg1GoZyH+7ljJ7N1jKnn/57N1sM7HLcZmHYTKfkpjvURESm6koOS s2zazU9GGtsMBKKDysHSGr94wY3yMObhvo8gRGNjwHt/ogbXoTFL4k98dmbqBxu+3koV gCoJHMLKQoZQBMvBmMAk/7Cy6nVU3Je6hRuR8gNjm8l/plZUCs71cNBV/IerHVE9nPu0 ss5Qq3X43trtpa9uhYac3z3ifzsaARDAa0hOYEiHooq6El2zHSl+wsroUeZvEaoRUymZ xcM6BSQwFD4ndgKRhFrRTJYXjnlhTU2oZ+hfpxPNjDL0FaL9ELEKLrV6Y9cuQngdfm2+ MOmA== MIME-Version: 1.0 In-Reply-To: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Date: Tue, 6 Nov 2012 01:32:09 +0000 Subject: Re: Multi-dimensional list initialization From: Oscar Benjamin To: Hans Mulder Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352165531 news.xs4all.nl 6889 [2001:888:2000:d::a6]:51517 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32791 On 5 November 2012 09:13, Hans Mulder wrote: > On 5/11/12 07:27:52, 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]] > > Or alternateively: > > m = [[None] * 4 for _ in range(4)] That's the way to do it. I've seen this question many times between here and the python-tutor list. It does seem to be a common gotcha. I was just thinking to myself that it would be a hard thing to change because the list would need to know how to instantiate copies of all the different types of the elements in the list. Then I realised it doesn't. It is simply a case of how the list multiplication operator is implemented and whether it chooses to use a reference to the same list or make a copy of that list. Since all of this is implemented within the same list type it is a relatively easy change to make (ignoring backward compatibility concerns). I don't see this non-copying list multiplication behaviour as contradictory but has anyone ever actually found a use for it? Oscar