Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-24 Thread python
 When I want to remove something from a list I typically write:

   while x in somelist:
   somelist.remove(x)

An O(n) version of removeall:

   somelist[:] = [e for e in somelist if e != x]


Raymond
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-23 Thread skip

Greg Or maybe remove() should just do nothing if the item is not
Greg found. 

If that's the case, I'd argue that dict.remove and set.remove should behave
the same way, making .discard unnecessary.  OTOH, perhaps lists should grow
a .discard method.

Skip
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-22 Thread Fredrik Lundh
Greg Ewing wrote:

 Actually I'd like this for lists. Often I find myself
 writing

   if x not in somelist:
 somelist.remove(x)

 A single method for doing this would be handy, and
 more efficient.

there is a single method that does this, of course, but you have to sprinkle
some sugar on it:

try:
somelist.remove(x)
except ValueError: pass

/F 



___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-22 Thread Greg Ewing
[EMAIL PROTECTED] wrote:

 It's obvious for sets and dictionaries that there is only one thing to
 discard and that after the operation you're guaranteed the key no longer
 exists.  Would you want the same semantics for lists or the semantics of
 list.remove where it only removes the first instance?

In my use cases I usually know that there is either
zero or one occurrences in the list.

But maybe it would be more useful to have a remove_all()
method, whose behaviour with zero occurrences would just
be a special case.

Or maybe remove() should just do nothing if the item is
not found. I don't think I've ever found getting an exception
from it to be useful, and I've often found it a nuisance.
What experiences have others had with it?

--
Greg
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] list.discard? (Re: dict.discard)

2006-09-21 Thread Greg Ewing
Gustavo Niemeyer wrote:

print set.discard.__doc__
   Remove an element from a set if it is a member.

Actually I'd like this for lists. Often I find myself
writing

   if x not in somelist:
 somelist.remove(x)

A single method for doing this would be handy, and
more efficient.

--
Greg
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-21 Thread Fred L. Drake, Jr.
On Thursday 21 September 2006 20:21, Greg Ewing wrote:
 if x not in somelist:
   somelist.remove(x)

I'm just guessing you really meant if x in somelist.  ;-)


  -Fred

-- 
Fred L. Drake, Jr.   fdrake at acm.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-21 Thread Josiah Carlson

Greg Ewing [EMAIL PROTECTED] wrote:
 Gustavo Niemeyer wrote:
 
 print set.discard.__doc__
Remove an element from a set if it is a member.
 
 Actually I'd like this for lists. Often I find myself
 writing
 
if x not in somelist:
  somelist.remove(x)
 
 A single method for doing this would be handy, and
 more efficient.

A marginal calling time improvement; but we are still talking linear
time containment test.

I'm -0, if only because I've never really found the need to use
list.remove(), so this API expansion doesn't feel necessary to me.

 - Josiah

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-21 Thread skip

Greg Actually I'd like [discard] for lists.

It's obvious for sets and dictionaries that there is only one thing to
discard and that after the operation you're guaranteed the key no longer
exists.  Would you want the same semantics for lists or the semantics of
list.remove where it only removes the first instance?

When I want to remove something from a list I typically write:

   while x in somelist:
   somelist.remove(x)

not if as in your example.

Skip
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] list.discard? (Re: dict.discard)

2006-09-21 Thread Steve Holden
Fred L. Drake, Jr. wrote:
 On Thursday 21 September 2006 20:21, Greg Ewing wrote:
  if x not in somelist:
somelist.remove(x)
 
 I'm just guessing you really meant if x in somelist.  ;-)
 
No you aren't, that's clearly an *informed* guess.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com