The ol' [[]] * 500 bug...

Discussion in 'Python' started by kj, Nov 13, 2009.

  1. kj

    kj Guest

    ....just bit me in the "fuzzy posterior". The best I can come up with
    is the hideous

    lol = [[] for _ in xrange(500)]

    Is there something better? What did one do before comprehensions
    were available? I suppose in that case one would have to go all
    the way with

    lol = [None] * 500
    for i in xrange(len(lol)):
    lol = []

    Yikes. 10 miles uphill, both ways...

    kynn
     
    kj, Nov 13, 2009
    #1
    1. Advertising

  2. kj

    Jon Clements Guest

    On 13 Nov, 21:26, kj <> wrote:
    > ...just bit me in the "fuzzy posterior".  The best I can come up with
    > is the hideous
    >
    >   lol = [[] for _ in xrange(500)]
    >
    > Is there something better?  


    That's generally the accepted way of creating a LOL.

    > What did one do before comprehensions
    > were available?  I suppose in that case one would have to go all
    > the way with
    >
    >   lol = [None] * 500
    >   for i in xrange(len(lol)):
    >       lol = []
    >
    > Yikes.  10 miles uphill, both ways...
    >


    >>> lol = map(lambda L: [], xrange(5))
    >>> [id(i) for i in lol]

    [167614956, 167605004, 167738060, 167737996, 167613036]

    Jon.
     
    Jon Clements, Nov 13, 2009
    #2
    1. Advertising

  3. kj

    kj Guest

    In <> Jon Clements <> writes:

    >>>> lol =3D map(lambda L: [], xrange(5))
    >>>> [id(i) for i in lol]

    >[167614956, 167605004, 167738060, 167737996, 167613036]


    Oh yeah, map! I'd forgotten all about it. Thanks!

    kynn
     
    kj, Nov 13, 2009
    #3
  4. kj schrieb:
    > ...just bit me in the "fuzzy posterior". The best I can come up with
    > is the hideous
    >
    > lol = [[] for _ in xrange(500)]


    If you call that hideous, I suggest you perform the same exercise in
    Java or C++ - and then come back to python and relax....


    Diez
     
    Diez B. Roggisch, Nov 14, 2009
    #4
  5. kj

    Paul Rubin Guest

    kj <> writes:
    > lol = [None] * 500
    > for i in xrange(len(lol)):
    > lol = []


    lol = map(list, [()] * 500)
     
    Paul Rubin, Nov 14, 2009
    #5
  6. 2009/11/14 Brian J Mingus <>:
    >
    >
    > On Sat, Nov 14, 2009 at 1:50 AM, Paul Rubin <http://>
    > wrote:
    >>
    >> kj <> writes:
    >> >   lol = [None] * 500
    >> >   for i in xrange(len(lol)):
    >> >       lol = []

    >>
    >> lol = map(list, [()] * 500)

    >
    > Could someone explain what the deal is with this thread? Thanks.
    > [[]]*500
    >


    Try
    >>> lst=[[]]*500
    >>> lst[7].append(2)
    >>> lst

    to see...

    vbr
     
    Vlastimil Brom, Nov 14, 2009
    #6
  7. Diez B. Roggisch wrote:
    > kj schrieb:
    >> lol = [[] for _ in xrange(500)]

    >
    > If you call that hideous, I suggest you perform the same exercise in
    > Java or C++ - and then come back to python and relax....


    I might be missing something that's not explicitly mentioned here, but I'd
    say that all non-associative containers in C++ have a resize() method, some
    even taking an initial size in their constructor.

    That said, [[]]*500 is IMHO more readable.

    Uli
     
    Ulrich Eckhardt, Nov 14, 2009
    #7
  8. kj

    Paul Rubin Guest

    Ulrich Eckhardt <> writes:
    > That said, [[]]*500 is IMHO more readable.


    But the issue in the thread is that it does the wrong thing.
     
    Paul Rubin, Nov 14, 2009
    #8
  9. On Fri, 13 Nov 2009 21:26:01 +0000, kj wrote:

    > ...just bit me in the "fuzzy posterior".


    It's not a bug. Just because it doesn't behave as you would like it to
    behave doesn't mean it isn't behaving as designed.


    > The best I can come up with is the hideous
    >
    > lol = [[] for _ in xrange(500)]


    That's not hideous.


    > Is there something better? What did one do before comprehensions were
    > available? I suppose in that case one would have to go all the way with
    >
    > lol = [None] * 500
    > for i in xrange(len(lol)):
    > lol = []
    >
    > Yikes. 10 miles uphill, both ways...


    What's wrong with that?


    lol = []
    for _ in xrange(500): lol.append([])


    is a simple alternative too, although the list comp is better.


    --
    Steven
     
    Steven D'Aprano, Nov 15, 2009
    #9
    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. yzhshi
    Replies:
    7
    Views:
    505
    yzhshi
    Apr 16, 2004
  2. Jesse Napier

    CustomErrors statusCode 500

    Jesse Napier, Aug 7, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,825
    Michal A. Valasek
    Aug 7, 2003
  3. Charlie Nilsson [MSFT]
    Replies:
    0
    Views:
    431
    Charlie Nilsson [MSFT]
    Aug 30, 2003
  4. James
    Replies:
    2
    Views:
    9,173
    Jacek Dziedzic
    Nov 3, 2004
  5. Spam Catcher
    Replies:
    4
    Views:
    505
    John Saunders [MVP]
    Jul 6, 2007
Loading...

Share This Page