Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99989

Re: Is there a way to set several list elements a same value with one line code

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
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 <mailman.188.1449190729.14615.python-list@python.org> (permalink)
References <d8efbad4-43d9-49ef-b0a9-dec1df52b3ab@googlegroups.com>
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 <python@mrabarnett.plus.com>
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 <d8efbad4-43d9-49ef-b0a9-dec1df52b3ab@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99989

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is there a way to set several list elements a same value with one line code Robert <rxjwg98@gmail.com> - 2015-12-03 16:30 -0800
  Re: Is there a way to set several list elements a same value with one line code MRAB <python@mrabarnett.plus.com> - 2015-12-04 00:58 +0000
    Re: Is there a way to set several list elements a same value with one line code Robert <rxjwg98@gmail.com> - 2015-12-03 18:11 -0800
  Re: Is there a way to set several list elements a same value with one line code Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-04 04:19 +0000

csiph-web