3 ms·
Not quite seamlessly, unfortunately. `l[:n]` gives you the first `n` elements of the list `l`. Ideally `l[-n:]` would give you the last `n` elements - but that
by pansa2 2y ago
Not quite seamlessly, unfortunately.
`l[:n]` gives you the first `n` elements of the list `l`. Ideally `l[-n:]` would give you the last `n` elements - but that doesn't work when `n` is zero.
I believe this is why C# introduced a special "index from end" operator, `^`, so you can refer to the end of the array as `^0`.
- UncleEntity 2y agoSo you're saying a negative index value should work like a count of elements to return and not an index? Then you couldn't do thing like l[-4:-2] to get a range of elements which seems slightly useful.
- pansa2 2y agoNo - negative indexing is fine as it is. You just need to be careful about the special case of negative zero.