<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 23, 2019 at 1:02 PM MRAB <<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2019-04-23 18:52, Terry Reedy wrote:<br>
> On 4/23/2019 2:44 AM, 林自均 wrote:<br>
>> Hi all,<br>
>> <br>
>> I found that there are str.index() and str.rindex(), but there is only <br>
>> list.index() and no list.rindex().<br>
> <br>
> str.index and list.index are related but not the same.  The consistency<br>
> argument is better applied to find-rfind, index-rindex,<br>
> partition-rpartition, etc.<br>
> <br>
> It is much more common to process strings right to left or in both<br>
> directions, than process lists right to left or in both directions.<br>
> Moreover, lists have a reverse method, strings do not.<br>
> ''.join(reversed(somestring)) is likely slower, especially if there many<br>
> non-ascii chars.  Moreover, somestring.rindex(substring) would have to<br>
> have both somestring and substring reversed when substring is more than<br>
> one char.<br>
> <br>
You can reverse a string with somestring[::-1].<br>
<br>
Personally, I'm not convinced by the "lists can be reversed" argument.<br></blockquote></div><div><br></div><div>Me neither, though for substring checks, reversing the string would be even more cumbersome (you'd have to reverse the query string too).</div><div><br></div><div>My money is on "nobody uses this for lists".</div><div><br></div><div>Some use cases for rindex() on strings that I found in a large codebase here include searching a pathname for the final slash, a list of comma-separated items for the last comma, a fully-qualified module name for the last period, and some ad-hoc parsing of other things. The "last separator" use cases are the most common and here rindex() sounds very useful.<br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div><div><i style="font-family:Arial,Helvetica,sans-serif;font-size:small;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);color:rgb(136,136,136)"><span>Pronouns</span>: he/him/his </i><a href="http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/" style="color:rgb(17,85,204);font-family:Arial,Helvetica,sans-serif;font-size:small;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)" target="_blank"><i>(why is my <span>pronoun</span> here?)</i></a></div></div></div></div>