Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'everybody,': 0.05; 'modify': 0.05; 'correct.': 0.07; 'objects,': 0.07; 'repeated': 0.07; 'python': 0.09; '[0,': 0.09; 'integer,': 0.09; 'integers': 0.09; 'sep': 0.09; 'subject:using': 0.09; '"*"': 0.16; '0],': 0.16; '[2,': 0.16; 'copied.': 0.16; 'integers,': 0.16; 'list;': 0.16; 'objects*': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'mostly': 0.20; 'subject:) ': 0.20; 'are.': 0.22; 'extends': 0.22; 'fine,': 0.22; 'received:74.125.82.174': 0.23; 'elements': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'subject:list': 0.28; '>>>>': 0.29; 'behavior.': 0.29; 'case,': 0.29; 'objects': 0.29; 'probably': 0.29; 'becomes': 0.30; 'lists': 0.31; 'subject:lists': 0.32; 'received:74.125.82': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'lists.': 0.35; 'pm,': 0.35; 'received:74.125': 0.36; 'subject:with': 0.36; 'subject: (': 0.36; 'two': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'matter': 0.61; 'first': 0.61; 'strange': 0.62; 'more': 0.63; 'behavior': 0.64; '26,': 0.65; 'conditions,': 0.84; 'to:name:python': 0.84; 'apparent': 0.91; 'do:': 0.91; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=l3jQ2qlJfQSQeCJmv0+CnFM+NLI6ObDjWLOEEmJmAGc=; b=h8xmDsVN9RRRFMFTIxCgQef0DGi5bJkS6+SwrwQjnRxQtAiYqeb/ikxA2yVVfxnvcx SeqbnpUhlgWipj4uJW24MQYeEaiwyJVeme9ncYA76xLprN8/UVEPRHMJfSnZgpd9ehO2 1TMF/RaVHViXGyRSvgOekgKnkBoRgWcX/oGfEXmxqZIi0a+mb6+nl23eAZSDL+b2u7Kc oX9BRcqgQrWQAUvwlLm2uLWCcPsHywC4QNAfLSvRpbG+vb+p/ThdUrVegs/2+dJtHrYS 4ATIbwPTsL0sHVkf8XeRji6ghAwsVKpCxKrnGalJHyqUakkM3cGpDCOLfVbdfZdf7T7u 9HMQ== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 26 Sep 2012 15:39:32 -0600 Subject: Re: using "*" to make a list of lists with repeated (and independent) elements To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348695604 news.xs4all.nl 6965 [2001:888:2000:d::a6]:52798 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30236 On Wed, Sep 26, 2012 at 3:20 PM, TP wrote: > Hi everybody, > > I have tried, naively, to do the following, so as to make lists quickly: > >>>> a=[0]*2 >>>> a > [0, 0] >>>> a[0]=3 >>>> a > [3, 0] > > All is working fine, so I extended the technique to do: > >>>> a=[[0]*3]*2 >>>> a > [[0, 0, 0], [0, 0, 0]] >>>> a[0][0]=2 >>>> a > [[2, 0, 0], [2, 0, 0]] > > The behavior is no more expected! > The reason is probably that in the first case, 0 is an integer, not a list, > so Python copies two elements that are independent. > In the second case, the elements are [0,0,0], which is a list; when Python > copies a list, he copies in fact the *pointer* to the list, such that we > obtain this apparently strange behavior. Mostly correct. When you do [foo] * 3 it extends the list with the *same objects* no matter what type they are. In the case of integers, it doesn't matter that it's the same objects, because integers are immutable. Lists are mutable, however, and so it becomes apparent that the same objects are repeated when you try to modify one of the lists. > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > without this behavior? Use a list comprehension: a = [[0] * 3 for _ in range(2)] This way the expression `[0] * 3` is re-evaluated at each position in the outer list, rather than evaluated just once and then copied.