<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Dec 30, 2012 at 5:03 AM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div>On 2012-12-30 03:25, David Kreuter wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hi python-ideas.<br>
<br>
I think it would be nice to have a method in 'list' to replace certain<br>
elements by others in-place. Like this:<br>
<br>
     l = [x, a, y, a]<br>
     l.replace(a, b)<br>
     assert l == [x, b, y, b]<br>
<br>
The alternatives are longer than they should be, imo. For example:<br>
<br>
     for i, n in enumerate(l):<br>
         if n == a:<br>
             l[i] = b<br>
<br>
Or:<br>
<br>
     l = [b if n==a else n for n in l]<br>
<br>
And this is what happens when someone tries to "optimize" this process.<br>
It totally obscures the intention:<br>
<br>
     try:<br>
         i = 0<br>
         while i < len(l):<br>
             i = l.index(a, i)<br>
             l[i] = b<br>
             i += 1<br>
     except ValueError:<br>
         pass<br>
<br>
If there is a reason not to add '.replace' as built-in method, it could<br>
be implemented in pure python efficiently if python provided a version<br>
of '.index' that returns the index of more than just the first<br>
occurrence of a given item. Like this:<br>
<br>
     l = [x, a, b, a]<br>
     for i in l.indices(a):<br>
         l[i] = b<br>
<br>
So adding .replace and/or .indices… Good idea? Bad idea?<br>
<br>
</blockquote></div></div>
What's your use-case?<br>
<br>
I personally can't remember ever needing to do this (or, if I have, it<br>
was so long ago that I can't remember it!).<br>
<br>
Features get added to Python only when someone can show a compelling<br>
reason for it and sufficient other people agree.<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</blockquote></div><br></div><div class="gmail_extra">When I write code for processing graphs it becomes very useful. For example:</div><div class="gmail_extra"><br></div><div class="gmail_extra">    def collapse_edge_undirected_graph(a, b):</div>

<div class="gmail_extra">        n = Node()</div><div class="gmail_extra">        n.connected = a.connected + b.connected</div><div class="gmail_extra">        for x in a.connected:</div><div class="gmail_extra">
            x.connected.replace(a, n)</div><div class="gmail_extra"><div class="gmail_extra">        for x in b.connected:</div><div class="gmail_extra">            x.connected.replace(b, n)</div><div class="gmail_extra">

<br></div><div class="gmail_extra">In other cases one would probably just add another layer of indirection.</div><div class="gmail_extra"><br></div><div class="gmail_extra">    x = Wrapper("a")<br></div>
<div class="gmail_extra">    y = Wrapper("y")<br></div><div class="gmail_extra">    a = Wrapper("a")</div><div class="gmail_extra"><br></div><div class="gmail_extra">    l = [x, a, y, a]</div><div class="gmail_extra">
    a.contents = "b" # instead of l.replace(a, b)</div><div class="gmail_extra"><br></div><div class="gmail_extra">But having to add .contents everywhere makes it messy. Graph code is complicated enough as it is.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra" style>And '.index' is a basically a resumable search. But instead of using a iterator-interface it requires the user to call it repeatedly.<br></div><div class="gmail_extra" style>
A method '.indices' returning a generator seems more like the python way to approaching this.</div>
</div></div>