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, 08:31 PM   #11
Default Re: Delete all items in the list


Terry Reedy wrote:
> 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.
>

But shouldn't that have been lambda x: 'a' != x, which would have
invoked the string (3.0, Unicode) comparison method?

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-27-2009, 12:27 AM   #12
Gabriel Genellina
 
Posts: n/a
Default Re: Delete all items in the list
En Thu, 26 Feb 2009 22:08:26 -0200, Peter Billam <>
escribió:

> On 2009-02-26, 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?

>
> L2 = list(set(L))
>
> works for me...


For a very strange definition of "works":

py> L = ['a','b','c','a','j','b','z','b','a']
py> L2
['a', 'c', 'b', 'z', 'j']
py> L2 = list(set(L))
py> L2
['a', 'c', 'b', 'z', 'j']

I still see an 'a', there are things missing, and the order is totally
lost.

--
Gabriel Genellina



Gabriel Genellina
  Reply With Quote
Old 02-27-2009, 12:29 AM   #13
Steve Holden
 
Posts: n/a
Default Re: Delete all items in the list
Peter Billam wrote:
> On 2009-02-26, 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?

>
> L2 = list(set(L))
>
> works for me...


I have to wonder for what value of "works" this works.

The simplest problem is it doesn't remove all the "a"s.

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-27-2009, 12:37 AM   #14
Gabriel Genellina
 
Posts: n/a
Default Re: Delete all items in the list
En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <>
escribió:
> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <>
> wrote:
>> On 2009-02-26, 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?

>>
>> L2 = list(set(L))
>>
>> works for me...

>
> A. That doesn't by itself remove all the 'a's, although it does remove
> all but 1 'a'
> B. That also removes all but one instance of *everything* in the list,
> not just 'a'
> C. That is lossy in that it completely loses the ordering of the
> original list


I said the very same things!
There are other issues too, like this only works with hashable objects,
but I've chosen exactly the same remarks and exactly in the same order!
What a coincidence...

--
Gabriel Genellina



Gabriel Genellina
  Reply With Quote
Old 02-27-2009, 12:39 AM   #15
Chris Rebert
 
Posts: n/a
Default Re: Delete all items in the list
On Thu, Feb 26, 2009 at 4:37 PM, Gabriel Genellina
<gagsl-> wrote:
> En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <>
> escribió:
>>
>> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <>
>> wrote:
>>>
>>> On 2009-02-26, 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?
>>>
>>> L2 = list(set(L))
>>>
>>> works for me...

>>
>> A. That doesn't by itself remove all the 'a's, although it does remove
>> all but 1 'a'
>> B. That also removes all but one instance of *everything* in the list,
>> not just 'a'
>> C. That is lossy in that it completely loses the ordering of the original
>> list

>
> I said the very same things!
> There are other issues too, like this only works with hashable objects, but
> I've chosen exactly the same remarks and exactly in the same order! What a
> coincidence...


Indeed, USENET and mail<->news lags doth create interesting situations
sometimes.

Cheers,
Chris

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


Chris Rebert
  Reply With Quote
Old 02-27-2009, 06:26 AM   #16
odeits
 
Posts: n/a
Default Re: Delete all items in the list
On Feb 26, 3:05*am, Clarendon <jine...@hotmail.com> 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?
> I would really appreciate your help.
>
> Thanks.


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

not the most efficient but it works


odeits
  Reply With Quote
Old 02-27-2009, 06:59 AM   #17
Chris Rebert
 
Posts: n/a
Default Re: Delete all items in the list
On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:
> On Feb 26, 3:05Â*am, Clarendon <jine...@hotmail.com> 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?
>> I would really appreciate your help.
>>
>> Thanks.

>
> while 'a' in L:
> Â* L.remove('a')
>
> not the most efficient but it works


"Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
= number of 'a's in L], versus just N. And that's not even accounting
for all the extra element movement done by .remove()

Cheers,
Chris

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


Chris Rebert
  Reply With Quote
Old 02-27-2009, 01:10 PM   #18
Steve Holden
 
Posts: n/a
Default Re: Delete all items in the list
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:

[...]
>> while 'a' in L:
>> L.remove('a')
>>
>> not the most efficient but it works

>
> "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
> = number of 'a's in L], versus just N. And that's not even accounting
> for all the extra element movement done by .remove()
>

Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in
assessing performance order?

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-27-2009, 01:46 PM   #19
Chris Rebert
 
Posts: n/a
Default Re: Delete all items in the list
On Fri, Feb 27, 2009 at 5:10 AM, Steve Holden <> wrote:
> Chris Rebert wrote:
>> On Thu, Feb 26, 2009 at 10:26 PM, odeits <> wrote:

> [...]
>>> while 'a' in L:
>>> Â* L.remove('a')
>>>
>>> not the most efficient but it works

>>
>> "Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
>> = number of 'a's in L], versus just N. And that's not even accounting
>> for all the extra element movement done by .remove()
>>

> Surely 2*O(M*N) is O(M*N) since constant factors are irrelevant in
> assessing performance order?


Obviously that equivalence is true, but in this case I'm emphasizing
that it's even worse than that when constant factors are taken into
account. Big-O is nice in the abstract, but in the real-world those
constant factors can matter.

In pure big-O, it is indeed O(M*N) vs. O(N)
Including constant factors, the performance is roughly 2*M*N*X [X =
overhead of remove()] vs. N, which makes the badness of the algorithm
all the more apparent.

Cheers,
Chris

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


Chris Rebert
  Reply With Quote
Old 02-28-2009, 10:15 AM   #20
Clarendon
 
Posts: n/a
Default Re: Delete all items in the list
Thank you very much for all your replies. I actually used the while
loop as the data is not large. But I was looking for a simpler, built
in command, something like L.remove('a', all) or L.removeall('a').
Python has re.findall function, but why not removeall, so users have
to make up long lines of expression?


Clarendon
  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