Deleting more than one element from a list

Emile van Sebille emile at fenx.com
Wed Apr 21 22:57:18 CEST 2010


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




More information about the Python-list mailing list