Delete all list entries of length unknown

Discussion in 'Python' started by flebber, Oct 5, 2009.

  1. flebber

    flebber Guest

    Hi

    Can someone clear up how I can remove all entries of a list when I am
    unsure how many entries there will be. I have been using sandbox to
    play essentially I am creating two lists a and b I then want to add a
    to b and remove all b entries. This will loop and b will receive new
    entries add it to a and delete again.

    I am going wrong with slice and list deletion, I assign x = len(b) and
    then attempting to delete based on this. Here is my sandbox session.
    What part am I getting wrong?

    #>>>
    a = (1, 2, 3, 4)
    b = (5, 6, 7, 8)
    #>>>
    a
    #---
    (1, 2, 3, 4)
    #>>>
    b
    #---
    (5, 6, 7, 8)
    #>>>
    a + b
    #---
    (1, 2, 3, 4, 5, 6, 7, 8)
    #>>>
    b
    #---
    (5, 6, 7, 8)
    #>>>
    len(b)
    #---
    4
    #>>>
    x = len(b)
    #>>>
    del b[0:x]
    Traceback (most recent call last):
    Error: File "<Shell>", line 1, in <module>
    Error: TypeError: 'tuple' object does not support item deletion
    #>>>
    b[0:x] = d
    Traceback (most recent call last):
    Error: File "<Shell>", line 1, in <module>
    Error: NameError: name 'd' is not defined
    #>>>
     
    flebber, Oct 5, 2009
    #1
    1. Advertising

  2. flebber

    r Guest

    On Oct 4, 10:09 pm, flebber <> wrote:
    > Hi
    >
    > Can someone clear up how I can remove all entries of a list when I am
    > unsure how many entries there will be.


    Sure...!

    >>> a = range(10)
    >>> a

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> del a[0]
    >>> a

    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> del a[-1]
    >>> a

    [1, 2, 3, 4, 5, 6, 7, 8]
    >>> del a[:]
    >>> a

    []

    or you could simple say

    >>> a = []


    ;-)
     
    r, Oct 5, 2009
    #2
    1. Advertising

  3. flebber

    Chris Rebert Guest

    On Sun, Oct 4, 2009 at 8:09 PM, flebber <> wrote:
    > Hi
    >
    > Can someone clear up how I can remove all entries of a list when I am
    > unsure how many entries there will be. I have been using sandbox to
    > play essentially I am creating two lists a and b I then want to add a
    > to b and remove all b entries. This will loop and b will receive new
    > entries add it to a and delete again.
    >
    > I am going wrong with slice and list deletion, I assign x = len(b) and
    > then attempting to delete based on this. Here is my sandbox session.
    > What part am I getting wrong?
    >
    > #>>>
    > a = (1, 2, 3, 4)
    > b = (5, 6, 7, 8)

    <snip>
    > x = len(b)
    > #>>>
    > del b[0:x]
    > Traceback (most recent call last):
    > Error:   File "<Shell>", line 1, in <module>
    > Error: TypeError: 'tuple' object does not support item deletion


    As the error message says, you're using *tuples*, not lists.
    Tuples are immutable (i.e. they cannot be modified after creation) and
    are created using parentheses.
    Lists on the other hand are made using brackets: [1, 2, 3, 4] # like this

    Additionally, the idiomatic way to clear a list (besides the more
    obvious approach of just assigning a new, empty list to the variable)
    is:
    del a[:]

    Leaving out the endpoints in the slice causes it to default to the
    entire contents of the list.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 5, 2009
    #3
  4. flebber

    Mark Tolonen Guest

    "Chris Rebert" <> wrote in message
    news:...
    > Tuples are immutable (i.e. they cannot be modified after creation) and
    > are created using parentheses.


    Slight correction: tuples are created using commas. Parentheses are only
    needed to disambiguate from other uses of comma:

    Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
    on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=1,
    >>> a

    (1,)
    >>> a=1,2,3
    >>> a

    (1, 2, 3)

    -Mark
     
    Mark Tolonen, Oct 5, 2009
    #4
  5. flebber

    flebber Guest

    On Oct 5, 3:05 pm, "Mark Tolonen" <> wrote:
    > "Chris Rebert" <> wrote in message
    >
    > news:...
    >
    > > Tuples are immutable (i.e. they cannot be modified after creation) and
    > > are created using parentheses.

    >
    > Slight correction: tuples are created using commas.  Parentheses are only
    > needed to disambiguate from other uses of comma:
    >
    > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
    > on win32
    > Type "help", "copyright", "credits" or "license" for more information.>>> a=1,
    > >>> a

    > (1,)
    > >>> a=1,2,3
    > >>> a

    >
    > (1, 2, 3)
    >
    > -Mark


    Awesome that has cleared it up for me, plus a bit more thanks.
     
    flebber, Oct 5, 2009
    #5
  6. flebber

    r Guest

    On Oct 4, 11:05 pm, "Mark Tolonen" <> wrote:
    > "Chris Rebert" <> wrote in message
    > > Tuples are immutable (i.e. they cannot be modified after creation) and
    > > are createdusingparentheses.

    >
    > Slight correction: tuples are createdusingcommas.  Parentheses are only
    > needed to disambiguate from other uses ofcomma:
    >
    > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
    > on win32
    > Type "help", "copyright", "credits" or "license" for more information.>>> a=1,
    > >>> a

    > (1,)
    > >>> a=1,2,3
    > >>> a

    >
    > (1, 2, 3)


    uhh? what python you using?

    >>> t = ()
    >>> t

    ()
    >>> type(t)

    <type 'tuple'>

    ;-)
     
    r, Oct 12, 2009
    #6
    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. Rick Ng Chi Wah
    Replies:
    2
    Views:
    355
    Ed Morton
    Jul 29, 2003
  2. M. Clift
    Replies:
    8
    Views:
    437
    M. Clift
    Aug 19, 2004
  3. Himanshu Chauhan

    Middle of a singly linked list of unknown length

    Himanshu Chauhan, Jun 6, 2008, in forum: C Programming
    Replies:
    15
    Views:
    1,120
    Kevin D. Quitt
    Jun 13, 2008
  4. Don Bruder
    Replies:
    3
    Views:
    1,019
    spikeysnack
    Aug 3, 2010
  5. Vincent Arnoux
    Replies:
    1
    Views:
    273
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page