3 ms·
If the order of the elements in a slice does not matter, this should be the easiest solution for deleting the element "n": slice[n] = slice[len(slice)-1] slic
by _ph_ 10y ago
If the order of the elements in a slice does not matter, this should be the easiest solution for deleting the element "n":
slice[n] = slice[len(slice)-1]
slice = slice[:len(slice)-1]
Probably one should campaign for adding a delete operator to slices, though so far I have never missed it.
- matthewmacleod 10y agoThis is a great example of the sort of thing that frustrates me when I'm using Go. Deleting an element from a slice is an obvious, common, simple operation. In order to do this, I have to build that operation from primitives, despite the fact that it will be more error-prone to do so. There are lots of other annoyances like this, too.