emptying a list

Discussion in 'Python' started by lallous, Oct 1, 2009.

  1. lallous

    lallous Guest

    Hello

    What is faster when clearing a list?

    del L[:]

    or

    L = []

    --
    Elias
     
    lallous, Oct 1, 2009
    #1
    1. Advertising

  2. "lallous" <> wrote in message news:ha2htc$u9u$...
    > Hello
    >
    > What is faster when clearing a list?
    >
    > del L[:]
    >
    > or
    >
    > L = []
    >


    Oh, "L = []" definitely, on the basis that there are fewer characters to
    type.


    http://docs.python.org/3.1/library/profile.html

    --
    Geoff
     
    Geoffrey Clements, Oct 1, 2009
    #2
    1. Advertising

  3. lallous

    Jon Clements Guest

    On 1 Oct, 16:30, "lallous" <> wrote:
    > Hello
    >
    > What is faster when clearing a list?
    >
    > del L[:]
    >
    > or
    >
    > L = []
    >
    > --
    > Elias


    Does it really matter that much?

    And you're really talking about two different things, which quite
    often come up on this group.

    Example follows:

    >>> x = range(5)
    >>> x = y
    >>> print x, y

    [1, 2, 3, 4] [1, 2, 3, 4]
    >>> x = []
    >>> print x, y

    [] [1, 2, 3, 4]
    >>> x = y
    >>> print x, y

    [1, 2, 3, 4] [1, 2, 3, 4]
    >>> del x[:]
    >>> print x, y

    [] []

    Cheers,
    Jon.
     
    Jon Clements, Oct 1, 2009
    #3
  4. lallous

    Simon Forman Guest

    On Thu, Oct 1, 2009 at 11:30 AM, lallous <> wrote:
    > Hello
    >
    > What is faster when clearing a list?
    >
    > del L[:]
    >
    > or
    >
    > L = []
    >
    > --
    > Elias
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    The first form actually clears the list, the second for just re-binds
    the name 'L' to a new, empty list.
     
    Simon Forman, Oct 2, 2009
    #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. Daren
    Replies:
    0
    Views:
    388
    Daren
    Dec 19, 2005
  2. Wendy Smoak

    cached backed or self-emptying map

    Wendy Smoak, Apr 29, 2005, in forum: Java
    Replies:
    2
    Views:
    469
    Wendy Smoak
    Apr 29, 2005
  3. Lee

    Stack emptying

    Lee, Sep 23, 2005, in forum: C Programming
    Replies:
    5
    Views:
    361
    Keith Thompson
    Sep 24, 2005
  4. J.M.
    Replies:
    18
    Views:
    592
    Howard Hinnant
    Feb 6, 2007
  5. Eric Lilja
    Replies:
    6
    Views:
    506
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Feb 15, 2007
Loading...

Share This Page