Is there a way to set several list elements a same value with one line code
Robert
rxjwg98 at gmail.com
Thu Dec 3 21:11:24 EST 2015
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.
More information about the Python-list
mailing list