Delete all items in the list

C

Clarendon

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

Chris Rebert

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
 
B

Bruno Desthuilliers

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']
 
B

Boris Borcic

Chris said:
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)
 
G

Gabriel Genellina

Chris said:
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.
 
J

John Posner

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

Steve Holden

Terry said:
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
 
T

Terry Reedy

Steve said:
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.
 
S

Steve Holden

Terry said:
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
 
G

Gabriel Genellina

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

Steve Holden

Peter said:
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
 
G

Gabriel Genellina

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

Chris Rebert

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
 
O

odeits

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
 
C

Chris Rebert

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
 
S

Steve Holden

Chris said:
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
 
C

Chris Rebert

Chris said:
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
 
C

Clarendon

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top