Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Is there a way to set several list elements a same value with one line code Date: Fri, 4 Dec 2015 00:58:39 +0000 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de +P9Qxlk/wUlo4Oobss/7rgGzV/8WSZBbtDCElg+QdivA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assignment': 0.07; 'subject:code': 0.07; 'subject:same': 0.09; 'subject:set': 0.09; 'syntax': 0.13; 'subject: \n ': 0.15; 'value.': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:several': 0.16; 'wrote:': 0.16; 'element': 0.18; '>>>': 0.20; 'code.': 0.23; 'elements': 0.23; 'forgot': 0.23; 'references': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'this.': 0.28; 'looks': 0.29; 'tutorial': 0.29; 'code': 0.30; 'guess': 0.31; 'received:84': 0.32; 'list': 0.34; 'robert': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'basic': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'several': 0.38; 'mean': 0.38; 'hi,': 0.38; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'here:': 0.63; 'subject:there': 0.66; 'as:': 0.79; 'application?': 0.84; 'now).': 0.84; 'subject:value': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=9CahGf4I12ovlqkOAUcA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99989 On 2015-12-04 00:30, Robert wrote: > Hi, > > I remember that there is a way to set several list elements a same value with > one line code. Excuse me, I don't remember the accurate syntax on the code > snippet. But the basic format looks like this. > > 1. There is a four-element list, such as: > bb=[[[]],[[]],[[]],[[]]] > 2. An assignment line is here: > bb[0]='a' > 3. Then, all 4 element of bb is set with the above value. > bb=[['a'],['a'],['a'],['a']] > > The above three line codes are what I guess (I forgot the original tutorial > now). Do you remember there is such a list application? > Do you mean this behaviour: >>> bb=[[[]]] * 4 >>> print(bb) [[[]], [[]], [[]], [[]]] >>> bb[0][0]='a' >>> print(bb) [['a'], ['a'], ['a'], ['a']] ? That's because the bb contains 4 references to the same list.