To prepend to a list, I use the_list[0:0]. But with slice appending:
Example:
results = [3,1,0]
results[-1:-1] = [3]
print(results)
>>> [3,1,3,0]
results[-0:-0] = 5
print(results)
>>> [5,3,1,3,0]
results = [3,1,0]
results[-1:-1] = [3]
print(results)
>>> [3,1,3,0]
results[-0:-0] = 5
print(results)
>>> [5,3,1,3,0]
Thoughts?
The slice that starts after the last element is len(…):.
results[len(results):] = [3]
but why, when there are += and extend?
results += [3]
results.extend([3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With