You wouldn’t be wrong, but manually doing the heavy lifting with the offset that would normally be handled by the >=
operator. Essentially, both yield the same result so we cannot say you are incorrect in any way. However, if you can save some logic by using an operator that does the job, let the operator do the work and skip the addition (an additional step). When effeciency matters (not now) the operator is more efficient.
if index >= len(my_list):
We notice you are using slicing, appending and concatenation, which is a lot of steps for such a low demand operation. All we really need to do in this function is validate the index (check that it is in range), and when valid, mutate the value at that index, then return the list.
n = len(my_list)
if -n <= index < n:
my_list[index] *= 2
return my_list
To illustrate, considering that Python reads negative indices as right to left, as opposed to the ‘normal’ left to right, the range can include negative numbers.
def double_index(my_list, index):
n = len(my_list)
if -n <= index < n:
my_list[index] *= 2
return my_list
>>> alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> double_index(alist, 8)
[1, 2, 3, 4, 5, 6, 7, 8, 18]
>>> double_index(alist, -1)
[1, 2, 3, 4, 5, 6, 7, 8, 18]
>>> double_index(alist, -9)
[2, 2, 3, 4, 5, 6, 7, 8, 9]
>>> double_index(alist, 0)
[2, 2, 3, 4, 5, 6, 7, 8, 9]
>>> double_index(alist, 9)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
On the whole, it doesn’t get much easier than that. Simple is always better, especially if it works correctly, all the time.
Extra study
>>> alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for x in range(-9, 9):
alist = double_index(alist, x) # preserve the mutated list
>>> alist
[4, 8, 12, 16, 20, 24, 28, 32, 36]
>>>
Because no exceptions are thrown, that means every value in our range is valid. The values in the initial list are all doubled twice, as a result. To repeat, the range()
object is valid as pertains to the length of alist
, proving our code model, itself, valid.
Bottom line, test, test, test. Test all your ideas and test them again. Compare iterations as you chalk in new changes, and how they test out. Our above model is a reduction to the least number of moving parts and simplest logic. It was with your model that we could derive this reduction. Now you’ve seen it done, once, continue along what ever line of thinking you begin with for each new idea.
Don’t try to write this reduced code from scratch as given in the above form. Write all kinds of code to fulfill your ideas, and then break it down, try refactoring in a small way, test again, refactor some more, test, test, test. One should not try to change the way one thinks, only learn how to reduce and refactor one’s ideas.
If we are always looking in our code for weakness, excess, repetition, uncertainty, we will find it (or not, toward the final iteration). Learning is about mitigation of our findings. It’s not the code that needs honing. It’s our intuition and knowledge assimilation. No limits on the mind, no limits on the potential. It is your potential you seek, not anyone else’s. No limits.