The ole Repetion != Concatination pitfall

Discussion in 'Python' started by JChG, Apr 19, 2010.

  1. JChG

    JChG Guest

    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.
    JChG, Apr 19, 2010
    #1
    1. Advertising

  2. On Sun, 18 Apr 2010 17:34:03 -0700, JChG wrote:

    > 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.



    Or when the objects are immutable, like ints, strings or None.


    pre_allocated = [None]*100
    while condition:
    i = get_index()
    pre_allocated = get_result()





    --
    Steven
    Steven D'Aprano, Apr 19, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    454
    metaperl
    Sep 15, 2006
  2. Marcin Kalicinski
    Replies:
    3
    Views:
    2,335
    Jonathan Mcdougall
    May 6, 2006
  3. Frederick Gotham

    Pitfall of proxy instead of temporary?

    Frederick Gotham, Aug 30, 2006, in forum: C++
    Replies:
    8
    Views:
    312
    Pete Becker
    Aug 31, 2006
  4. Kira Yamato
    Replies:
    28
    Views:
    781
    James Kanze
    Sep 27, 2007
  5. Odalrick

    weakref pitfall

    Odalrick, Oct 20, 2007, in forum: Python
    Replies:
    3
    Views:
    302
    Peter Otten
    Oct 20, 2007
Loading...

Share This Page