Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Delete all items in the list

Reply
Thread Tools

Delete all items in the list

 
 
Clarendon
Guest
Posts: n/a
 
      02-26-2009
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.

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-26-2009
On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      02-26-2009
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']

 
Reply With Quote
 
Boris Borcic
Guest
Posts: n/a
 
      02-26-2009
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <(E-Mail Removed)> 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)

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-26-2009
En Thu, 26 Feb 2009 11:00:30 -0200, Boris Borcic <(E-Mail Removed)>
escribió:

> Chris Rebert wrote:
>> On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <(E-Mail Removed)> 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

 
Reply With Quote
 
John Posner
Guest
Posts: n/a
 
      02-26-2009
>>
>> > 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/
 
Reply With Quote
 
John Posner
Guest
Posts: n/a
 
      02-26-2009
>>
>> 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/
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-26-2009
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'.

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-26-2009
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/

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-26-2009
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to retrieve listbox items on postback , items moved usingjavascript between 2 list boxes (source and target ) divya ASP .Net 1 05-28-2008 05:27 AM
Is there any way to append some items to List box, without retrieving all items through AJAX? Anjan Bhowmik ASP .Net 1 02-14-2008 09:02 PM
How to delete and remove all items in a container<T*> James Aguilar C++ 8 03-22-2005 07:19 PM
pmw MenuBar: delete all menu items of a menu Tina Li Python 0 09-18-2003 09:57 PM
grouping items among a list according to items subtag value Gilles Kuhn XML 0 09-15-2003 12:01 PM



Advertisments