Deleting more than one element from a list

Discussion in 'Python' started by candide, Apr 21, 2010.

  1. candide

    candide Guest

    Is the del instruction able to remove _at the same_ time more than one
    element from a list ?


    For instance, this seems to be correct :


    >>> z=[45,12,96,33,66,'ccccc',20,99]
    >>> del z[2], z[6],z[0]
    >>> z

    [12, 33, 66, 'ccccc', 20]
    >>>



    However, the following doesn't work :

    >> z=[45,12,96,33,66,'ccccc',20,99]
    >>> del z[2], z[3],z[6]

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>>



    Does it mean the instruction

    del z[2], z[3],z[6]

    to be equivalent to the successive calls


    del z[2]
    del z[3]
    del z[6]

    ?
    candide, Apr 21, 2010
    #1
    1. Advertising

  2. On 21 April 2010 20:56, candide <> wrote:
    > Is the del instruction able to remove _at the same_ time more than one
    > element from a list ?


    Yup:

    >>> z=[45,12,96,33,66,'ccccc',20,99]
    >>> del z[:]
    >>> z

    []

    --
    Cheers,
    Simon B.
    Simon Brunning, Apr 21, 2010
    #2
    1. Advertising

  3. candide

    Gary Herron Guest

    candide wrote:
    > Is the del instruction able to remove _at the same_ time more than one
    > element from a list ?
    >
    >
    > For instance, this seems to be correct :
    >
    >
    > >>> z=[45,12,96,33,66,'ccccc',20,99]
    > >>> del z[2], z[6],z[0]
    > >>> z

    > [12, 33, 66, 'ccccc', 20]
    > >>>

    >
    >
    > However, the following doesn't work :
    >
    > >> z=[45,12,96,33,66,'ccccc',20,99]
    > >>> del z[2], z[3],z[6]

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > IndexError: list assignment index out of range
    > >>>

    >
    >
    > Does it mean the instruction
    >
    > del z[2], z[3],z[6]
    >
    > to be equivalent to the successive calls
    >
    >
    > del z[2]
    > del z[3]
    > del z[6]


    Yes, those are equivalent. The reason it fails is that, by the time it
    gets around to the third delete, there is no longer in index [6] in the
    list. The element you were thinking of is now at index [4].

    This, however, will work as you expected:

    del z[6], z[3],z[2]




    --
    Gary Herron, PhD.
    Department of Computer Science
    DigiPen Institute of Technology
    (425) 895-4418
    Gary Herron, Apr 21, 2010
    #3
  4. candide

    Mensanator Guest

    On Apr 21, 2:56 pm, candide <> wrote:
    > Is the del instruction able to remove _at the same_ time more than one
    > element from a list ?
    >
    > For instance, this seems to be correct :
    >
    >  >>> z=[45,12,96,33,66,'ccccc',20,99]
    >  >>> del z[2], z[6],z[0]
    >  >>> z
    > [12, 33, 66, 'ccccc', 20]
    >  >>>
    >
    > However, the following doesn't work :
    >
    >  >> z=[45,12,96,33,66,'ccccc',20,99]
    >  >>> del z[2], z[3],z[6]
    > Traceback (most recent call last):
    >    File "<stdin>", line 1, in <module>
    > IndexError: list assignment index out of range
    >  >>>
    >
    > Does it mean the instruction
    >
    > del z[2], z[3],z[6]
    >
    > to be equivalent to the successive calls
    >
    > del z[2]
    > del z[3]
    > del z[6]


    That's part of the problem. Let's look at a better example.

    >>> z = [0,1,2,3,4,5,6]
    >>> del z[0],z[3],z[6]

    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    del z[0],z[3],z[6]
    IndexError: list assignment index out of range
    >>> z

    [1, 2, 3, 5, 6]

    Yes, the error was caused by the list shrinking between calls,
    so the 6 did not get deleted. But notice that 3 is still there
    and 4 is missing.

    If you must delete this way, do it bottom up so that the index
    remains valid for the subsequent calls:

    >>> z = [0,1,2,3,4,5,6]
    >>> del z[6],z[3],z[0]
    >>> z

    [1, 2, 4, 5]


    >
    > ?
    Mensanator, Apr 21, 2010
    #4
  5. On 4/21/2010 12:56 PM candide said...
    > Is the del instruction able to remove _at the same_ time more than one
    > element from a list ?
    >
    >
    > For instance, this seems to be correct :
    >
    >
    > >>> z=[45,12,96,33,66,'ccccc',20,99]


    Not as I see it -- watch your index values - they change after each
    delete is completed. It'll work if you order them backwards though.

    >>> a = range(10)
    >>> del a[0],a[2],a[4],a[6]
    >>> a

    [1, 2, 4, 5, 7, 8]
    >>> a = range(10)
    >>> del a[6],a[4],a[2],a[0]
    >>> a

    [1, 3, 5, 7, 8, 9]
    >>>


    Emile
    Emile van Sebille, Apr 21, 2010
    #5
  6. candide

    candide Guest

    Thanks for your reponses.
    candide, Apr 21, 2010
    #6
  7. On Apr 21, 12:56 pm, candide <> wrote:
    > Is the del instruction able to remove _at the same_ time more than one
    > element from a list ?
    >
    > For instance, this seems to be correct :
    >
    >  >>> z=[45,12,96,33,66,'ccccc',20,99]
    >  >>> del z[2], z[6],z[0]
    >  >>> z
    > [12, 33, 66, 'ccccc', 20]
    >  >>>
    >
    > However, the following doesn't work :
    >
    >  >> z=[45,12,96,33,66,'ccccc',20,99]
    >  >>> del z[2], z[3],z[6]
    > Traceback (most recent call last):
    >    File "<stdin>", line 1, in <module>
    > IndexError: list assignment index out of range
    >  >>>
    >
    > Does it mean the instruction
    >
    > del z[2], z[3],z[6]
    >
    > to be equivalent to the successive calls
    >
    > del z[2]
    > del z[3]
    > del z[6]
    >
    > ?


    Looks like you got a lot of good answers to the question as asked.

    FWIW, successive delete operations on a list are dog slow.
    It is better to delete all of the entries in one pass.
    There are several ways to do it. Here's one:

    >>> z=[45,12,96,33,66,'ccccc',20,99]
    >>> targets = [2, 3, 6]
    >>> PLACEHOLDER = object()
    >>> for i in targets:

    .... z = PLACEHOLDER
    >>> z[:] = [elem for elem in z if elem is not PLACEHOLDER]


    Here's another:

    >>> z=[45,12,96,33,66,'ccccc',20,99]
    >>> targets = set([2, 3, 6])
    >>> z[:] = [elem for i, elem in enumerate(z) if i not in targets]


    Besides being scaleable, these two examples have some nice learning
    points. Hopefully, you will find them useful.


    Raymond
    Raymond Hettinger, Apr 25, 2010
    #7
    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. Chuckk Hubbard
    Replies:
    1
    Views:
    304
    Peter Otten
    Jun 9, 2008
  2. Merciadri Luca
    Replies:
    4
    Views:
    802
  3. Bob Rashkin
    Replies:
    5
    Views:
    66
    Dennis Lee Bieber
    Dec 23, 2013
  4. Steven D'Aprano
    Replies:
    0
    Views:
    79
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    74
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page