With the list: [-1, 0, 43, 128, 32]
, there are a couple ways of removing the final element.
list.pop()
list = list[:-1]
(not recommended?)del list[-1]
- And probably a couple more...
They would all return [-1, 0, 43, 128]
, but what's least computationally intensive and does it make a difference? I'm aware of modules like timeit
which I could use to test this for myself. But I'm wary of uncontrolled variables and my non-expertise could definitely dilute the results. As well, does the best option differ for strings, or floats, or booleans? What about multidimensional lists?
I'm not quite sure how to control and test these variables, so I'd figure I'd ask here to see if there is a general hierarchy.
Not a Duplicate of Difference between del, remove and pop on lists
That question explains the differences between methods of deletion, but does not address slicing. It also does not address speed at all. The accepted answer makes vague mentions to efficiency which I can see being part of the solution, but I do not see how it fits with slicing.