Re: Python Enhancement Proposal for List methods

2018-10-22 Thread Bob Gailer
Many of the features of python were added not out of need but rather for
convenience.

If need were the only criteria we could immediately get rid of
comprehensions, since they can be written as a series of for loops, the
same can be said for many other newer features.

Python is all about making the programming experience easier and more
productive. Expanding replace  from just strings to other sequences such as
as lists makes sense. The cost in time and effort to add this to python is
trivial.

I say go for it.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Python Enhancement Proposal for List methods

2018-10-22 Thread Schachner, Joseph
I agree with others that I don't see a compelling need to add these to Python, 
since they are all easy to implement in a few lines.

But what I really want to say is that Python tries hard to be easily readable 
and easily understood, by any reader.  List.removeall( ) that does not remove 
all elements, but all occurrences of a value passed to it, does not seem to me 
to meet that goal.  Even if you implement this as a function I encourage to 
think of a better name than removeall.  

--- Joseph S.



-Original Message-
From: Siva Sukumar Reddy  
Sent: Sunday, October 21, 2018 8:37 AM
To: python-list@python.org; python-id...@python.org
Subject: Python Enhancement Proposal for List methods

Hey everyone,

I am really new to Python contribution community want to propose below methods 
for List object. Forgive me if this is not the format to send an email.

1. *list.replace( item_to_be_replaced, new_item )*: which replaces all the 
occurrences of an element in the list instead of writing a new list 
comprehension in place.
2. *list.replace( item_to_be_replaced, new_item, number_of_occurrences )*:
which replaces the occurrences of an element in the list till specific number 
of occurrences of that element. The number_of_occurrences can defaulted to 0 
which will replace all the occurrences in place.
3. *list.removeall( item_to_be_removed )*: which removes all the occurrences of 
an element in a list in place.

What do you think about these features?
Are they PEP-able? Did anyone tried to implement these features before?
Please let me know.

Thank you,
Sukumar

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Enhancement Proposal for List methods

2018-10-22 Thread Chris Angelico
On Mon, Oct 22, 2018 at 11:20 PM Lutz Horn  wrote:
>
> On Sun, Oct 21, 2018 at 06:06:40PM +0530, Siva Sukumar Reddy wrote:
> > 1. *list.replace( item_to_be_replaced, new_item )*: which replaces all the
> > occurrences of an element in the list instead of writing a new list
> > comprehension in place.
>
> Try this:
>
> >>> l = [1, 3, 4, 5, 6, 5, 4, 3, 2, 1]
> >>> def replace(l, old, new):
> ... try:
> ... while True:
> ... l[l.index(old)] = new
> ... except ValueError:
> ... pass
> ...
> ...
> >>> replace(l, 5, 55)
> >>> l
> [1, 3, 4, 55, 6, 55, 4, 3, 2, 1]
>
> Functions like this are simple to implement. There is no need to add
> them to the stdlib.
>

And simple to get wrong, hence my recommendation for a recipe in the
docs. For example, your replace() function will get into an infinite
loop if old and new compare equal to each other. It's also going to
start the search over from the beginning every time it finds
something, which is wasteful.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Enhancement Proposal for List methods

2018-10-22 Thread Lutz Horn
On Sun, Oct 21, 2018 at 06:06:40PM +0530, Siva Sukumar Reddy wrote:
> 1. *list.replace( item_to_be_replaced, new_item )*: which replaces all the
> occurrences of an element in the list instead of writing a new list
> comprehension in place.

Try this:

>>> l = [1, 3, 4, 5, 6, 5, 4, 3, 2, 1]
>>> def replace(l, old, new):
... try:
... while True:
... l[l.index(old)] = new
... except ValueError:
... pass
... 
... 
>>> replace(l, 5, 55)
>>> l
[1, 3, 4, 55, 6, 55, 4, 3, 2, 1]

Functions like this are simple to implement. There is no need to add
them to the stdlib.

Lutz
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Enhancement Proposal for List methods

2018-10-22 Thread Peter Otten
Siva Sukumar Reddy wrote:

> I am really new to Python contribution community want to propose below
> methods for List object. Forgive me if this is not the format to send an
> email.
> 
> 1. *list.replace( item_to_be_replaced, new_item )*: which replaces all the
> occurrences of an element in the list instead of writing a new list
> comprehension in place.
> 2. *list.replace( item_to_be_replaced, new_item, number_of_occurrences )*:
> which replaces the occurrences of an element in the list till specific
> number of occurrences of that element. The number_of_occurrences can
> defaulted to 0 which will replace all the occurrences in place.
> 3. *list.removeall( item_to_be_removed )*: which removes all the
> occurrences of an element in a list in place.
> 
> What do you think about these features?
> Are they PEP-able? Did anyone tried to implement these features before?
> Please let me know.

You can easily implement those as functions.

def list_replace(items, old, new, count=None):
indices = (i for i, v in enumerate(items) if v == old)
if count is not None:
indices = itertools.islice(indices, count)
for index in indices:
items[index] = new

def list_removeall(items, value):
for index in reversed(range(len(items))):
if items[index] == value:
del items[index]

To promote them to methods of a built-in class you need to provide use-cases
that are both frequent and compelling, I think.

So what are your use-cases?

-- 
https://mail.python.org/mailman/listinfo/python-list