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


Groups > comp.lang.python > #99988 > unrolled thread

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

Started byRobert <rxjwg98@gmail.com>
First post2015-12-03 16:30 -0800
Last post2015-12-04 04:19 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#99988 — Is there a way to set several list elements a same value with one line code

FromRobert <rxjwg98@gmail.com>
Date2015-12-03 16:30 -0800
SubjectIs there a way to set several list elements a same value with one line code
Message-ID<d8efbad4-43d9-49ef-b0a9-dec1df52b3ab@googlegroups.com>
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?


Thanks,

[toc] | [next] | [standalone]


#99989

FromMRAB <python@mrabarnett.plus.com>
Date2015-12-04 00:58 +0000
Message-ID<mailman.188.1449190729.14615.python-list@python.org>
In reply to#99988
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.

[toc] | [prev] | [next] | [standalone]


#99991

FromRobert <rxjwg98@gmail.com>
Date2015-12-03 18:11 -0800
Message-ID<46a3ad9e-02f9-4bb0-9981-e7dd8fcb8f30@googlegroups.com>
In reply to#99989
On Thursday, December 3, 2015 at 7:59:16 PM UTC-5, MRAB wrote:
> 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.

Yes! What you post is I want. Thanks.

[toc] | [prev] | [next] | [standalone]


#99995

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-12-04 04:19 +0000
Message-ID<n3r48c$svs$2@dont-email.me>
In reply to#99988
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web