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

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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-27-2009
En Thu, 26 Feb 2009 22:08:26 -0200, Peter Billam <(E-Mail Removed)>
escribiˇ:

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

>
> 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

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      02-27-2009
Peter Billam wrote:
> On 2009-02-26, 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?

>
> 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/

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-27-2009
En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <(E-Mail Removed)>
escribiˇ:
> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <(E-Mail Removed)>
> wrote:
>> On 2009-02-26, 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?

>>
>> 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

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-27-2009
On Thu, Feb 26, 2009 at 4:37 PM, Gabriel Genellina
<(E-Mail Removed)> wrote:
> En Thu, 26 Feb 2009 22:18:18 -0200, Chris Rebert <(E-Mail Removed)>
> escribi├│:
>>
>> On Thu, Feb 26, 2009 at 4:08 PM, Peter Billam <(E-Mail Removed)>
>> wrote:
>>>
>>> On 2009-02-26, 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?
>>>
>>> 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
 
Reply With Quote
 
odeits
Guest
Posts: n/a
 
      02-27-2009
On Feb 26, 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?
> I would really appreciate your help.
>
> Thanks.


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

not the most efficient but it works
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-27-2009
On Thu, Feb 26, 2009 at 10:26 PM, odeits <(E-Mail Removed)> wrote:
> On Feb 26, 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?
>> 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
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-27-2009
Chris Rebert wrote:
> On Thu, Feb 26, 2009 at 10:26 PM, odeits <(E-Mail Removed)> 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/

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-27-2009
On Fri, Feb 27, 2009 at 5:10 AM, Steve Holden <(E-Mail Removed)> wrote:
> Chris Rebert wrote:
>> On Thu, Feb 26, 2009 at 10:26 PM, odeits <(E-Mail Removed)> 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
 
Reply With Quote
 
Clarendon
Guest
Posts: n/a
 
      02-28-2009
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?
 
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