No, no such built-in method exists for list
s. It is trivial to find out if a list index is valid, so no function is needed. You could just put the code in your function (or the even more readable s[ind] if ind < len(s) else 0
) directly into the two or three places it's needed, and it would be perfectly understandable.
You can also use the fact that slicing a list always succeeds, take a one-item slice, make an iterator of that, and try to get the next value from the iterator. Then you can specify the default value on the next()
call.
next(iter(s[ind:ind+1]), 0)
But this is going to be both slower and less clear than the if
construction.
Of course, both your code and mine assume ind
is always positive...
If you do want to write a function, you could make it a method of a list
subclass.