Preallocate? -- potentially brain dead question about performance

Discussion in 'Python' started by Jan Danielsson, Feb 26, 2007.

  1. Hello all,

    I have a list which contains a bunch of tuples:

    mylist = [ ('1', 'Foobar'), ('32', 'Baz'), ('4', 'Snorklings') ]

    (The list can potentially be shorter, or much longer). Now I want to
    take the first element in each tuple and store it in a list (to use in a
    Set later on).

    Which would You prefer of the following:

    newlist = [ ]
    for e in mylist:
    newlist.append(int(e[0]))

    ..or..

    newlist = [ None ] * len(mylist)
    for i in range(len(mylist)):
    newlist.append(int(e[0]))


    To me, the second one is more appealing, when I think in terms of
    risk of memory fragmentation, etc. But since Python is such a high level
    language, I'm not sure my traditional reasoning applies.

    --
    Kind regards,
    Jan Danielsson
    Jan Danielsson, Feb 26, 2007
    #1
    1. Advertising

  2. Jan Danielsson

    Guest

    Jan Danielsson:
    > newlist = [ None ] * len(mylist)
    > for i in range(len(mylist)):
    > newlist.append(int(e[0]))


    Note that this appends after the Nones, not over them. And note that
    here the name 'e' is undefined.

    The most used idiom for that is:

    newlist = [int(e[0]) for e in mylist]

    Or better lazily if you want to create a set, avoiding the list
    creation:

    newlist = set(int(e[0]) for e in mylist)

    Bye,
    bearophile
    , Feb 26, 2007
    #2
    1. Advertising

  3. wrote:
    >> newlist = [ None ] * len(mylist)
    >> for i in range(len(mylist)):
    >> newlist.append(int(e[0]))

    >
    > Note that this appends after the Nones, not over them. And note that
    > here the name 'e' is undefined.


    What an idiot I am.. Yes, I know that.. And I actually *had* working
    code laying around, but rather than opening it and copy-and-pasting, I
    simply rewrote it. I obviously meant:

    newlist = int(e[0])

    > The most used idiom for that is:
    >
    > newlist = [int(e[0]) for e in mylist]


    > Or better lazily if you want to create a set, avoiding the list
    > creation:
    >
    > newlist = set(int(e[0]) for e in mylist)


    ...completely avoiding the design issue I raised altogether. Thanks!
    Exactly what I was hoping for! :)

    --
    Kind regards,
    Jan Danielsson
    Jan Danielsson, Feb 27, 2007
    #3
  4. Jan Danielsson

    Guest

    Jan Danielsson:
    > ...completely avoiding the design issue I raised altogether. Thanks!
    > Exactly what I was hoping for! :)


    Another common way to do it, it may be a little slower because that
    n,txt is a tuple:

    items = set(int(n) for n,txt in mylist)

    Bye,
    bearophile
    , Feb 27, 2007
    #4
    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. Lloyd Sheen

    Brain Dead Reformatting

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    333
    Lloyd Sheen
    Jan 24, 2004
  2. Replies:
    2
    Views:
    315
    S. Justin Gengo
    Apr 24, 2006
  3. Kamilche

    Brain Dead Singleton

    Kamilche, Jun 4, 2004, in forum: Python
    Replies:
    10
    Views:
    471
    Peter Otten
    Jun 8, 2004
  4. pataphor
    Replies:
    0
    Views:
    423
    pataphor
    Jun 14, 2009
  5. Stephanie Stowe

    brain dead cookie question

    Stephanie Stowe, Apr 6, 2004, in forum: ASP General
    Replies:
    3
    Views:
    100
    Stephanie Stowe
    Apr 7, 2004
Loading...

Share This Page