Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'initialize': 0.05; 'none,': 0.05; "'a'": 0.07; 'hardcoded': 0.09; 'throws': 0.09; 'url:github': 0.09; "['a',": 0.16; '[none]': 0.16; 'distinct': 0.16; 'elements,': 0.16; 'libs': 0.16; 'none]': 0.16; 'sense,': 0.16; 'creates': 0.18; '>>>': 0.18; 'to:name:python- list@python.org': 0.20; 'subject:skip:i 10': 0.22; 'elements': 0.23; 'references': 0.23; 'seems': 0.23; 'skip:@ 10': 0.27; 'subject:list': 0.28; 'behaviour': 0.29; 'array': 0.29; "i'm": 0.29; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'so,': 0.35; 'expected': 0.35; 'received:192.168.0': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'too': 0.36; 'charset:us-ascii': 0.36; 'received:209': 0.37; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'easy': 0.60; 'header:Message-Id:1': 0.62; 'here': 0.65; '"oh,': 0.84; 'nice,': 0.84; 'why?': 0.84; '(running': 0.91; 'received:192.168.0.100': 0.91; 'was:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:content-transfer-encoding:subject:message-id:date :to:mime-version:x-mailer; bh=dXPswijlnJS8/ZMUVzEjtXvoSihl1Z3vd3spuXDquHg=; b=yThdH5otGKj7Z4o4/Bd7DlArl8QKc87TwI6qIbgjfZxBOMwL3WS+RrT3Paj/UwLYjt Zr+y+LOYgJwLueQeN1xPrnF4qHYbm5e/TwgH10txDulqPNE7gbrgLyAj7CDGjk6AJOyy H47aLsSzsdDxwkqYd6iZJjJ5H7HM0cFvbPIq4YsgIMswRsdx3GomXp2h+8+zdcJxWq5h WhB2jFOYqeHbiML5LKf9O0ilIulSKZCg5ZKYyQfsBLUek9WmEoxlzSNadbGZ2NrF/7is EzpKeM80L/HQTYCRw4t06yXjSnaqZFDPk46j58VYOU+0Lc8GNvqdRDApwX2Tw/GNINlF Irlg== From: Demian Brecht Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Multi-dimensional list initialization Date: Sun, 4 Nov 2012 22:27:52 -0800 To: "python-list@python.org" Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) X-Mailer: Apple Mail (2.1499) 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352096879 news.xs4all.nl 6908 [2001:888:2000:d::a6]:51229 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32748 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 =3D [[None] * 4] * 4 The way to get what I was after was: m =3D [[None] * 4, [None] * 4, [None] * 4, [None * 4]]=20 (Obviously, I could have just hardcoded the initialization, but I'm too = lazy to type all that out ;)) The behaviour I encountered seems a little contradictory to me. [None] * = 4 creates four distinct elements in a single array while [[None] * 4] * = 4 creates one distinct array of four distinct elements, with three = references to it: >>> a =3D [None] * 4 >>> a[0] =3D 'a' >>> a ['a', None, None, None] >>> m =3D [[None] * 4] * 4 >>> m[0][0] =3D 'm' >>> m [['m', None, None, None], ['m', None, None, None], ['m', None, None, = None], ['m', None, None, None]] Is this expected behaviour and if so, why? In my mind either result = makes sense, but the inconsistency is what throws me off. Demian Brecht @demianbrecht http://demianbrecht.github.com