The ole Repetion != Concatination pitfall

JChG jchgingerich at gmail.com
Sun Apr 18 20:34:03 EDT 2010


Well I started learning Python last week, and in my first experiment I
got caught when I changed:

sieve = [ {1:True} for x in range(r)]
to
sieve = [{1:True}] * r

I was expecting it to be equivalent to
sieve = [{1:True},{1:True},...]
but instead it's
t = [{1:True}]; sieve = [t,t,...]

Okay, I see this was discussed 13 years ago, and it's a deliberate
choice.  There are other ways to do this.

But I'll still whine anyway...I'm not seeing where list repetition is
particularly useful, except when you want independent objects, like my
initialization of sieve above.  Oh well, I guess I'll just avoid it.



More information about the Python-list mailing list