On 2019-04-02 19:10, Stephen J. Turnbull wrote:
Anders Hovmöller writes:
Removing "file:" prefix: https://github.com/merijn/dotfiles/blob/43c736c73c5eda413dc7b4615bb679bd43a1... <https://github.com/merijn/dotfiles/blob/43c736c73c5eda413dc7b4615bb679bd43a1...>
This is interesting, because it shows the (so far standard) one-liner:
word[len(prefix):] if word.startswith(prefix) else word
can be improved (?!) to
word[len(prefix) if word.startswith(prefix) else 0:]
It could be 'improved' more to: word[word.startswith(prefix) and len(prefix) : ]
I don't know if this is more readable, but I think it's less so.
Note that version 1 doesn't copy word if it doesn't start with prefix, while version 2 does. In many applications I can think of the results would be accumulated in a set, and version 1 equality tests will also be faster in the frequent case that the word doesn't start with the prefix. So that's the one I'd go with, as I can't think of any applications where multiple copies of the same string would be useful.
_Neither_ version copies if the word doesn't start with the prefix. If you won't believe me, test them! :-)