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


Groups > comp.lang.python > #99995

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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Denis McMahon <denismfmcmahon@gmail.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 04:19:24 -0000 (UTC)
Organization A noiseless patient Spider
Lines 37
Message-ID <n3r48c$svs$2@dont-email.me> (permalink)
References <d8efbad4-43d9-49ef-b0a9-dec1df52b3ab@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Injection-Date Fri, 4 Dec 2015 04:19:24 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="66ffcfa4470a58bcddbdcd1913f98ab4"; logging-data="29692"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CZUjRyJEQm5CCfw0SOOPzv/azdbT/npI="
User-Agent Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2)
Cancel-Lock sha1:kTDtl7YhaJcAMI8bVVitxLBDve0=
Xref csiph.com comp.lang.python:99995

Show key headers only | View raw


On Thu, 03 Dec 2015 16:30:25 -0800, 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?

bb = [<something> for i in range(<whatever>)]

will create bb as a list of size whatever elements each of which is 
<something>

eg:

>>> bb = [ ['a'] for i in range(4)]
>>> bb
[['a'], ['a'], ['a'], ['a']]
>>> bb = [ 0 for i in range(5)]
>>> bb
[0, 0, 0, 0, 0]
>>> 

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.python | Previous | NextPrevious 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