Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Delete all items in the list

 
Thread Tools Search this Thread
Old 02-26-2009, 11:05 AM   #1
Default Delete all items in the list


Hi. This must be a simple command but I just can't find it in the
Phthon manual. How do I delete all items with a certain condition from
a list? For instance:

L=['a', 'b', 'c', 'a']

I want to delete all 'a's from the list.
But if L.remove('a') only deletes the first 'a'.

How do you delete all 'a's?
I would really appreciate your help.

Thanks.



Clarendon
  Reply With Quote
Old 02-26-2009, 11:15 AM   #2
Chris Rebert
 
Posts: n/a
Default Re: Delete all items in the list
On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:
> Hi. This must be a simple command but I just can't find it in the
> Phthon manual. How do I delete all items with a certain condition from
> a list? For instance:
>
> L=['a', 'b', 'c', 'a']
>
> I want to delete all 'a's from the list.
> But if L.remove('a') only deletes the first 'a'.
>
> How do you delete all 'a's?


There are several ways. I'd go with a list comprehension:

L = [i for i in L if i != 'a']

Or to modify the list in-place:

L[:] = [i for i in L if i != 'a']

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com


Chris Rebert
  Reply With Quote
Old 02-26-2009, 12:48 PM   #3
Bruno Desthuilliers
 
Posts: n/a
Default Re: Delete all items in the list
Clarendon a écrit :
> Hi. This must be a simple command but I just can't find it in the
> Phthon manual. How do I delete all items with a certain condition from
> a list? For instance:
>
> L=['a', 'b', 'c', 'a']
>
> I want to delete all 'a's from the list.
> But if L.remove('a') only deletes the first 'a'.
>
> How do you delete all 'a's?


L[:] = [item for item in L if item != 'a']



Bruno Desthuilliers
  Reply With Quote
Old 02-26-2009, 01:00 PM   #4
Boris Borcic
 
Posts: n/a
Default Re: Delete all items in the list
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:

....
>> L=['a', 'b', 'c', 'a']
>>
>> I want to delete all 'a's from the list.
>> But if L.remove('a') only deletes the first 'a'.
>>
>> How do you delete all 'a's?

>
> There are several ways. I'd go with a list comprehension:


and for a couple other ways

while 'a' in L : L.remove('a')

L = filter('a'.__ne__,L)



Boris Borcic
  Reply With Quote
Old 02-26-2009, 03:26 PM   #5
Gabriel Genellina
 
Posts: n/a
Default Re: Delete all items in the list
En Thu, 26 Feb 2009 11:00:30 -0200, Boris Borcic <>
escribió:

> Chris Rebert wrote:
>> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <> wrote:

> ...
>>> L=['a', 'b', 'c', 'a']
>>>
>>> I want to delete all 'a's from the list.
>>> But if L.remove('a') only deletes the first 'a'.
>>>
>>> How do you delete all 'a's?

>> There are several ways. I'd go with a list comprehension:

>
> and for a couple other ways
>
> while 'a' in L : L.remove('a')


This is probably the worst way, takes cuadratic time (and two searches per
loop).

> L = filter('a'.__ne__,L)


And this is probably the fastest. But not all types define __ne__ so a
more generic answer would be:

from functools import partial
from operator import ne
L = filter(partial(ne, 'a'), L)

Of course, this is only relevant if L is large and there are many
duplicates. In other cases I'd stick with the list comprehension as posted
by Chris Rebert.

--
Gabriel Genellina



Gabriel Genellina
  Reply With Quote
Old 02-26-2009, 03:44 PM   #6
John Posner
 
Posts: n/a
Default RE: Delete all items in the list
>>
>> > L = filter('a'.__ne__,L)

>>
>> And this is probably the fastest. But not all types define
>> __ne__ so a
>> more generic answer would be:
>>
>> from functools import partial
>> from operator import ne
>> L = filter(partial(ne, 'a'), L)
>>


And don't forget this "traditional" solution:

>>> L=['a', 'b', 'c', 'a']
>>> filter(lambda arg: arg != 'a', L)

['b', 'c']

-John





E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.11850
http://www.pctools.com/en/spyware-doctor-antivirus/


John Posner
  Reply With Quote
Old 02-26-2009, 03:52 PM   #7
John Posner
 
Posts: n/a
Default functools.partial (was: Delete all items in the list)
>>
>> from functools import partial
>> from operator import ne
>> L = filter(partial(ne, 'a'), L)
>>


I learned about functools.partial only recently (on this list). functools is
implemented in C, not Python, so I couldn't answer this question for myself:

Is functools.partial completely equivalent to a manually-coded closure?

Help, please.

-John





E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.11850
http://www.pctools.com/en/spyware-doctor-antivirus/


John Posner
  Reply With Quote
Old 02-26-2009, 06:22 PM   #8
Terry Reedy
 
Posts: n/a
Default Re: Delete all items in the list
Gabriel Genellina wrote:

>
>> L = filter('a'.__ne__,L)

>
> And this is probably the fastest. But not all types define __ne__


In Py3, all classes inherit .__ne__ from 'object'.



Terry Reedy
  Reply With Quote
Old 02-26-2009, 06:42 PM   #9
Steve Holden
 
Posts: n/a
Default Re: Delete all items in the list
Terry Reedy wrote:
> Gabriel Genellina wrote:
>
>>
>>> L = filter('a'.__ne__,L)

>>
>> And this is probably the fastest. But not all types define __ne__

>
> In Py3, all classes inherit .__ne__ from 'object'.
>

Isn't that inherited method just an identity comparison?

However in this particular case the main point is that str *does*
implement __ne__. So as long as that built-in method doesn't call the
other operand's __ne__ there should be no problem.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/



Steve Holden
  Reply With Quote
Old 02-26-2009, 08:27 PM   #10
Terry Reedy
 
Posts: n/a
Default Re: Delete all items in the list
Steve Holden wrote:
> Terry Reedy wrote:
>> Gabriel Genellina wrote:
>>
>>>> L = filter('a'.__ne__,L)
>>> And this is probably the fastest. But not all types define __ne__

>> In Py3, all classes inherit .__ne__ from 'object'.
>>

> Isn't that inherited method just an identity comparison?


Yes. And so is, by default, the snipped slower proposed alternative of
lambda x: a!= x.

> However in this particular case the main point is that str *does*
> implement __ne__. So as long as that built-in method doesn't call the
> other operand's __ne__ there should be no problem.
>
> regards
> Steve




Terry Reedy
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't delete file or folder? nork Software 1 11-02-2009 11:52 AM
asp.net : custom code to delete data from Grid view (Database) sara_23apr Software 1 06-19-2007 01:08 PM
Update and Delete From Gridview Usman Software 0 11-01-2006 10:05 AM
How do I: Correctly deny the delete permission? sundevilkid85 Software 0 05-08-2006 09:49 PM
Latest Tech Fiasco... Ghost A+ Certification 30 01-09-2004 12:15 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46