355
fmt.Println("Enter position to delete::")
fmt.Scanln(&pos)

new_arr := make([]int, (len(arr) - 1))
k := 0
for i := 0; i < (len(arr) - 1); {
    if i != pos {
        new_arr[i] = arr[k]
        k++
        i++
    } else {
        k++
    }
}

for i := 0; i < (len(arr) - 1); i++ {
    fmt.Println(new_arr[i])
}

I am using this command to delete an element from a Slice but it is not working, please suggest.

3

24 Answers 24

496

Order matters

If you want to keep your array ordered, you have to shift all of the elements at the right of the deleting index by one to the left. Hopefully, this can be done easily in Golang:

func remove(slice []int, s int) []int {
    return append(slice[:s], slice[s+1:]...)
}

However, this is inefficient because you may end up with moving all of the elements, which is costly.

Order is not important

If you do not care about ordering, you have the much faster possibility to replace the element to delete with the one at the end of the slice and then return the n-1 first elements:

func remove(s []int, i int) []int {
    s[i] = s[len(s)-1]
    return s[:len(s)-1]
}

With the reslicing method, emptying an array of 1 000 000 elements take 224s, with this one it takes only 0.06ns.

This answer does not perform bounds-checking. It expects a valid index as input. This means that negative values or indices that are greater or equal to the initial len(s) will cause Go to panic.

Slices and arrays being 0-indexed, removing the n-th element of an array implies to provide input n-1. To remove the first element, call remove(s, 0), to remove the second, call remove(s, 1), and so on and so forth.

13
  • 3
    Hm, not really. This: s[i] = s[len(s)-1] definitely copies the last element to the element at index i. Then, return s[:len(s)-1] returns the slice without the last element. Two statements there. Commented Dec 7, 2016 at 19:02
  • 3
    Fails with len(arr) == 2 and the elem to delete is the last one: play.golang.org/p/WwD4PfUUjsM
    – zenocon
    Commented Oct 21, 2018 at 20:27
  • 8
    @zenocon In Golang, arrays are 0-index, meaning that valid indices for an array of length 2 are 0 and 1. Indeed, this function does not check the bounds of the array, and expects a valid index to be provided. When len(arr) == 2, valid arguments are thus 0 or 1. Anything else would trigger an out of bounds access, and Go will panic. Commented Nov 1, 2018 at 8:37
  • 10
    Adding this for reference, for the order does not matter option, it's better to use s[len(s)-1], s[i] = 0, s[len(s)-1]. Especially so if you're working with non-primitive arrays. If you had pointers to something it's better to make the element you want to remove nil before slicing so you don't have pointers in the underlying array. This answer explains why very well. In short: after moving the last element at the deleted element's location, zero the last element before slicing.
    – Vlad V
    Commented Feb 16, 2020 at 16:22
  • 23
    This is a dangerous answer and I’m guessing has introduced a few hundred bugs into programs that didn’t fully understand that this modified all references to the slice. It’s also not correct if you read through the original question which you can see wants to preserve the original slice. Commented Apr 18, 2020 at 18:09
162

This is a little strange to see but most answers here are dangerous and gloss over what they are actually doing. Looking at the original question that was asked about removing an item from the slice a copy of the slice is being made and then it's being filled. This ensures that as the slices are passed around your program you don't introduce subtle bugs.

Here is some code comparing users answers in this thread and the original post. Here is a go playground to mess around with this code in.

Append based removal

package main

import (
    "fmt"
)

func RemoveIndex(s []int, index int) []int {
    return append(s[:index], s[index+1:]...)
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    removeIndex := RemoveIndex(all, 5)

    fmt.Println("all: ", all) //[0 1 2 3 4 6 7 8 9 9]
    fmt.Println("removeIndex: ", removeIndex) //[0 1 2 3 4 6 7 8 9]

    removeIndex[0] = 999
    fmt.Println("all: ", all) //[999 1 2 3 4 6 7 9 9]
    fmt.Println("removeIndex: ", removeIndex) //[999 1 2 3 4 6 7 8 9]
}

In the above example you can see me create a slice and fill it manually with numbers 0 to 9. We then remove index 5 from all and assign it to remove index. However when we go to print out all now we see that it has been modified as well. This is because slices are pointers to an underlying array. Writing it out to removeIndex causes all to be modified as well with the difference being all is longer by one element that is no longer reachable from removeIndex. Next we change a value in removeIndex and we can see all gets modified as well. Effective go goes into some more detail on this.

The following example I won't go into but it does the same thing for our purposes. And just illustrates that using copy is no different.

package main

import (
    "fmt"
)

func RemoveCopy(slice []int, i int) []int {
    copy(slice[i:], slice[i+1:])
    return slice[:len(slice)-1]
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    removeCopy := RemoveCopy(all, 5)

    fmt.Println("all: ", all) //[0 1 2 3 4 6 7 8 9 9]
    fmt.Println("removeCopy: ", removeCopy) //[0 1 2 3 4 6 7 8 9]

    removeCopy[0] = 999
    fmt.Println("all: ", all) //[99 1 2 3 4 6 7 9 9]
    fmt.Println("removeCopy: ", removeCopy) //[999 1 2 3 4 6 7 8 9]
}

The questions original answer

Looking at the original question it does not modify the slice that it's removing an item from. Making the original answer in this thread the best so far for most people coming to this page.

package main

import (
    "fmt"
)

func OriginalRemoveIndex(arr []int, pos int) []int {
    new_arr := make([]int, (len(arr) - 1))
    k := 0
    for i := 0; i < (len(arr) - 1); {
        if i != pos {
            new_arr[i] = arr[k]
            k++
        } else {
            k++
        }
        i++
    }

    return new_arr
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    originalRemove := OriginalRemoveIndex(all, 5)

    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    fmt.Println("originalRemove: ", originalRemove) //[0 1 2 3 4 6 7 8 9]

    originalRemove[0] = 999
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    fmt.Println("originalRemove: ", originalRemove) //[999 1 2 3 4 6 7 8 9]
}

As you can see this output acts as most people would expect and likely what most people want. Modification of originalRemove doesn't cause changes in all and the operation of removing the index and assigning it doesn't cause changes as well! Fantastic!

This code is a little lengthy though so the above can be changed to this.

A correct answer

package main

import (
    "fmt"
)

func RemoveIndex(s []int, index int) []int {
    ret := make([]int, 0)
    ret = append(ret, s[:index]...)
    return append(ret, s[index+1:]...)
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    removeIndex := RemoveIndex(all, 5)

    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    fmt.Println("removeIndex: ", removeIndex) //[0 1 2 3 4 6 7 8 9]

    removeIndex[0] = 999
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    fmt.Println("removeIndex: ", removeIndex) //[999 1 2 3 4 6 7 8 9]
}

Almost identical to the original remove index solution however we make a new slice to append to before returning.

1
  • 26
    this should be the answer for the question since its the only one that explains the risk of modifying the slice's backing array
    – JessG
    Commented Apr 18, 2020 at 17:59
94

Remove one element from the Slice (this is called 're-slicing'):

package main

import (
    "fmt"
)

func RemoveIndex(s []int, index int) []int {
    return append(s[:index], s[index+1:]...)
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println(all) //[0 1 2 3 4 5 6 7 8 9]
    all = RemoveIndex(all, 5)
    fmt.Println(all) //[0 1 2 3 4 6 7 8 9]
}
6
  • 14
    Worth pointing out that this is called 're-slicing' and is relatively expensive though it is the idiomatic way to do this in Go. Just don't confuse this with an operation like removing a node from a linked list because it's not that and if you're going to do it a lot, especially with large collections, you should consider alternative designs that avoid it. Commented May 19, 2016 at 22:24
  • Yes this is idiomatic way in Golang, and even in C/Assembly removing one random element from sorted array is expensive, you need shift (copy) all right elements one position to the left. Yes in some use case Linked List is better solution to remove random element from the List.
    – user6169399
    Commented May 20, 2016 at 7:15
  • 4
    Note that this method causes all to be modified and now n and all point to part of the same underlying array. This is very likely to lead to bugs in your code. Commented Jul 26, 2019 at 5:10
  • 1
    Getting this error 2019/09/28 19:46:25 http: panic serving 192.168.1.3:52817: runtime error: slice bounds out of range [7:5] goroutine 7 [running]: Commented Sep 28, 2019 at 14:18
  • 7
    @STEEL are you sure about that? --> play.golang.org/p/IzaGUJI4qeG Commented Aug 6, 2021 at 9:46
76

Using Delete from the slices package (stable since Go 1.21, on older Go versions you have to import golang.org/x/exp/slices):

slice := []int{1, 2, 3, 4}
slice = slices.Delete(slice, 1, 2)
fmt.Println(slice) // [1 3 4]

Go playground example

slices.Delete(s, i, j) removes the elements s[i:j] from s

  • i.e. the elements from and including index i up to and excluding index j
  • or if you remember math notation for intervals: [i,j)

Note two things:

  • Delete modifies the contents of the original slice
  • You need to reassign slice, otherwise it will have the wrong length
9
  • 13
    It is a wrapper function for this append(slice[:s], slice[s+1:]...). More info: cs.opensource.google/go/x/exp/+/0b5c67f0:slices/slices.go;l=156 Commented Jun 6, 2022 at 9:50
  • 1
    The source code states that "Delete modifies the contents of the slice s; it does not create a new slice." but it's not obvious that you need to reassign the modified slice to update the length. This should really be highlighted. Commented Dec 12, 2022 at 20:25
  • @TomBöttger: Please help me improve my answer and let me know: What is not clear enough about the phrase "You need to reassign slice, otherwise it will have the wrong length"?
    – Alex80286
    Commented Dec 13, 2022 at 21:51
  • 1
    @Alex80286 I'm sorry, my comment was quite ambiguous. Your answer is perfectly fine! It's good that you mention that the slice needs to be reassigned since the go source code doesn't mention it (at least not directly). Commented Dec 13, 2022 at 21:59
  • This function does not modify the backing array of the slice! Meaning the "deleted" elements are still inside the backing array. This is no different than re-indexing a slice. Commented Aug 14, 2023 at 11:58
32

Minor point (code golf), but in the case where order does not matter you don't need to swap the values. Just overwrite the array position being removed with a duplicate of the last position and then return a truncated array.

func remove(s []int, i int) []int {
    s[i] = s[len(s)-1]
    return s[:len(s)-1]
}

Same result.

4
  • 15
    The most readable implementation would be to copy the first element to the specified index s[i] = s[0], then return an array with only the last n-1 elements. return s[1:]
    – Kent
    Commented Feb 9, 2017 at 21:35
  • 4
    playground of @Kent 's solution Commented Mar 16, 2018 at 21:14
  • 6
    @Kent the issue with doing s[1:] versus s[:len(s)-1] is that the later will perform much better if the slice later gets appended or deletes are intermingled with appends. The later keeps the slice capacity where-as the former doesn't.
    – Dave C
    Commented Aug 18, 2019 at 11:24
  • 1
    If you remove the 0th element with this function, it inverts the result.
    – ivarec
    Commented Oct 27, 2019 at 23:55
31

This is how you Delete From a slice the idiomatic way. You don't need to build a function it is built into the append. Try it here https://play.golang.org/p/QMXn9-6gU5P

z := []int{9, 8, 7, 6, 5, 3, 2, 1, 0}
fmt.Println(z)  //will print Answer [9 8 7 6 5 3 2 1 0]

z = append(z[:2], z[4:]...)
fmt.Println(z)   //will print Answer [9 8 5 3 2 1 0]
23

From the book The Go Programming Language

To remove an element from the middle of a slice, preserving the order of the remaining elements, use copy to slide the higher-numbered elements down by one to fill the gap:

func remove(slice []int, i int) []int {
  copy(slice[i:], slice[i+1:])
  return slice[:len(slice)-1]
}
6
  • 7
    Note that this method causes the original slice passed in to be modified. Commented Jul 26, 2019 at 5:10
  • @eatingthenight append can modify the backing array, so why can't remove? go.dev/play/p/9eQStgiqYYK
    – blackbox
    Commented Feb 11, 2022 at 21:03
  • @blackbox That's not right, append doesn't modify the backing array. Your code assigned the value of append to the backing array s2 = append(s2, 2) and if you omit this step, the backing array s2 remains unchanged. Compare with go.dev/play/p/7EubNjwOFuD Commented Jul 14, 2022 at 2:31
  • @TomAnderson append modifies the backing array when the capacity is greater than the length will be. Here's my original example without assigning the result of append to s2 go.dev/play/p/mz095qeCnvL. Here's your example with a greater capacity explicitly set go.dev/play/p/v0JR88kuDyB. And here's a more in depth example go.dev/play/p/sj95rpeaJRB.
    – blackbox
    Commented Aug 5, 2022 at 22:58
  • @blackbox if you remove the :cap(s2) from the print statements in go.dev/play/p/v0JR88kuDyB, then it appears that s2 is not being updated. So is it safe to say that the backing array is updated, but the slice is not? Commented Aug 25, 2022 at 15:25
17

I take the below approach to remove the item in slice. This helps in readability for others. And also immutable.

func remove(items []string, item string) []string {
    newitems := []string{}

    for _, i := range items {
        if i != item {
            newitems = append(newitems, i)
        }
    }

    return newitems
}
3
  • I like this approach better you are actually removing all occurrences of the item.
    – eexit
    Commented Jul 7, 2020 at 20:27
  • 2
    if you have over 20k records or more this wont scale, because you will need to cycle through n times before you get - linear search wont cut it.
    – Dele
    Commented Aug 21, 2022 at 10:33
  • As Dele says above, this isn't at all scalable, but it is elegant and clean. In a production environment, you'll likely not use approaches like this, but for small environments this is fine. Commented Aug 21, 2023 at 7:42
14

The best way to do it is to use the append function:

package main

import (
    "fmt"
)

func main() {
    x := []int{4, 5, 6, 7, 88}
    fmt.Println(x)
    x = append(x[:2], x[4:]...)//deletes 6 and 7
    fmt.Println(x)
}

https://play.golang.org/p/-EEFCsqse4u

13

Using generics you can pass any type of slice.

// Removes slice element at index(s) and returns new slice
func remove[T any](slice []T, s int) []T {
    return append(slice[:s], slice[s+1:]...)
}

Usage

slice := []int{1, 2, 3, 4}
result := remove(slice, 0)
fmt.Println(result)
// [2 3 4]

Example
https://go.dev/play/p/LhPGvEuZbRA

8

The currently most voted answer by T. Claverie is correct but I find the algorithm more clear if swap is performed only if needed, i.e. for all but the last element of the slice. This can be achieved by a simple if guard.

Order is not important/ no boundary checks perfomed

func remove(s []int, i int) []int {
    // bring element to remove at the end if it's not there yet
    if i != len(s)-1 {
        s[i] = s[len(s)-1]
    }
 
    // drop the last element
    return s[:len(s)-1]
}
7

As of Go 1.22, you no longer have to do this yourself. The team have introduced the type agnostic Delete() function for slices. To delete an element or set of elements, pass in the slice to be modified along with a start an end index for items to be removed.

mySlice = slices.Delete(myslice, 1, 1)
1
  • The correct would be mySlice = slices.Delete(myslice, 1, 2) Commented Jun 4 at 13:24
5

Find a way here without relocating.

  • changes order
a := []string{"A", "B", "C", "D", "E"}
i := 2

// Remove the element at index i from a.
a[i] = a[len(a)-1] // Copy last element to index i.
a[len(a)-1] = ""   // Erase last element (write zero value).
a = a[:len(a)-1]   // Truncate slice.

fmt.Println(a) // [A B E D]
  • keep order
a := []string{"A", "B", "C", "D", "E"}
i := 2

// Remove the element at index i from a.
copy(a[i:], a[i+1:]) // Shift a[i+1:] left one index.
a[len(a)-1] = ""     // Erase last element (write zero value).
a = a[:len(a)-1]     // Truncate slice.

fmt.Println(a) // [A B D E]
3

Maybe you can try this method:

// DelEleInSlice delete an element from slice by index
//  - arr: the reference of slice
//  - index: the index of element will be deleted
func DelEleInSlice(arr interface{}, index int) {
    vField := reflect.ValueOf(arr)
    value := vField.Elem()
    if value.Kind() == reflect.Slice || value.Kind() == reflect.Array {
        result := reflect.AppendSlice(value.Slice(0, index), value.Slice(index+1, value.Len()))
        value.Set(result)
    }
}

Usage:

arrInt := []int{0, 1, 2, 3, 4, 5}
arrStr := []string{"0", "1", "2", "3", "4", "5"}
DelEleInSlice(&arrInt, 3)
DelEleInSlice(&arrStr, 4)
fmt.Println(arrInt)
fmt.Println(arrStr)

Result:

0, 1, 2, 4, 5
"0", "1", "2", "3", "5"
3
  • 1
    Likely because it's not idiomatic and over-engineered for what the question is asking. It's an interesting way to solve it but no one should be using this. Commented Jul 26, 2019 at 4:10
  • 1
    Thanks! Actually worked just fine for me with an Interface, since this seems to be universal, maybe
    – Edw590
    Commented Dec 21, 2019 at 13:10
  • After studying a bit more of Go, I now have a question about this. Shouldn't this not include the Array type there? Arrays can't be modified in size, like in C, so that probably won't work (it did not with me at least). I believe the function could be reduced to 2 lines (which would also partially optimize it - less variables declared and less operations to do): var value reflect.Value = reflect.ValueOf(array).Elem(); value.Set(reflect.AppendSlice(value.Slice(0, index), value.Slice(index+1, value.Len()))).
    – Edw590
    Commented Nov 10, 2021 at 17:12
2
for index, item := range movies{
    if item.ID == "123"{
        movies = append(movies[:index], movies[index+1:]...)
        break
    }
}
1

Maybe this code will help.

It deletes item with a given index.

Takes the array, and the index to delete and returns a new array pretty much like append function.

func deleteItem(arr []int, index int) []int{
  if index < 0 || index >= len(arr){
    return []int{-1}
  }

    for i := index; i < len(arr) -1; i++{
      arr[i] = arr[i + 1]

    }

    return arr[:len(arr)-1]
}

Here you can play with the code : https://play.golang.org/p/aX1Qj40uTVs

1

In the language tutorial we learn that:

Slices are like references to arrays. A slice does not store any data, it just describes a section of an underlying array. Changing the elements of a slice modifies the corresponding elements of its underlying array.

For this reason, working with the append function on slices without taking care of the origin and destination of the values we are dealing with is very dangerous as well as wrong for the Go philosophy.

The correct solution is therefore to work with a slice referenced to a new array and not the "main" one. This is possible by creating a new slice through the make construct.

func removeAt(slice []int, index int) []int {
    newSlice := make([]int, 0) //Create a new slice of type []int and length 0
    newSlice = append(newSlice, slice[:index]...) //Copies the values contained in the old slice to the new slice up to the index (excluded)
    if index != len(slice)-1 {
        newSlice = append(newSlice, slice[index+1:]...) //If the index to be removed was different from the last one, then proceed to copy the following values of the index to the end of the old slice
    }
    return newSlice
}

In this way we are able to safely remove an element of a slice, regardless of the use we will make on the return of the function.


Since I used a function to answer the question, it would be a good idea to handle any errors as follows:

func removeAt(slice []int, index int) ([]int, error) {
    if index < 0 {
        return nil, fmt.Errorf("index (%d) cannot be a negative number", index)
    }
    if index >= len(slice) {
        return nil, fmt.Errorf("index (%d) cannot be a number greater or equal than the length of slice (%d)", index, len(slice))
    }

    newSlice := make([]int, 0)
    newSlice = append(newSlice, slice[:index]...)
    if index != len(slice)-1 {
        newSlice = append(newSlice, slice[index+1:]...)
    }
    
    return newSlice, nil
}

Or better yet, implement the function that can handle multiple types through interfaces. However, all this is a good practice since you build a function to do this, which does not concern the question posed.

However, an example of a test on the Go playground can be found here.

1

My two cents here, after the introduction of generics the lodash library has become way too powerful therefore:

mySlice = lo.Without[T](myslice, mySlice[indexOfTheElementToRemove])

where T the type of mySlice (if it is not a custom type I think it is automatically inferred so the T may not be needed).

0

No need to check every single element unless you care contents and you can utilize slice append. try it out

pos := 0
arr := []int{1, 2, 3, 4, 5, 6, 7, 9}
fmt.Println("input your position")
fmt.Scanln(&pos)
/* you need to check if negative input as well */
if (pos < len(arr)){
    arr = append(arr[:pos], arr[pos+1:]...)
} else {
    fmt.Println("position invalid")
}
0

You need to change your code a little bit,

new_arr := make([]int, (len(arr) - 1))
for i := 0; i < len(arr); i++ {
    if i != pos {
        new_arr = append(new_arr, arr[i])
    }
}

For a more efficient loop you can use this

for i, item := range arr {
    ...
}

At last you can do it by using native slice functionality

new_arr = append(arr[:2], arr[3:])

The last solution remove element in the index 2 and put the new slice in new_arr.

0

This is the correct and best way to delete an element from the list.Because other ways can shift the elements while deleting. So try following method if Order matters.

 func remove(s []int, index int) []int{{ return append(s[:index], s[index+1:]...) }
0

Basically you just assign to your slice the same slice but one item shorter:

data = data[:len(data)-1]

-3

Since Slice is backed by an array and since there is no way you can remove an element from an array and not reshuffle memory;and I did not want to do that ugly code; here is a pseudo code to keep an index for removed items; Basically I wanted an ordered slice where position mattered even after delete

type ListSlice struct {
  sortedArray []int
  deletedIndex map[int]bool
}
func lenSlice(m ListSlice)int{
    return len(m.sortedArray)
}
func deleteSliceElem(index int,m ListSlice){
    m.deletedIndex[index]=true
}
func getSliceElem(m ListSlice,i int)(int,bool){
    _,deleted :=m.deletedIndex[i]
    return m.sortedArray[i],deleted
}
for i := 0; i < lenSlice(sortedArray); i++ {
        
        k,deleted := getSliceElem(sortedArray,i)
        if deleted {continue}
        ....
        deleteSliceElem(i,sortedArray)

}

m := ListSlice{sortedArray: []int{5, 4, 3},deletedIndex: make(map[int]bool) }
...
-4

here is the playground example with pointers in it. https://play.golang.org/p/uNpTKeCt0sH

package main

import (
    "fmt"
)

type t struct {
    a int
    b string
}

func (tt *t) String() string{
    return fmt.Sprintf("[%d %s]", tt.a, tt.b)
}

func remove(slice []*t, i int) []*t {
  copy(slice[i:], slice[i+1:])
  return slice[:len(slice)-1]
}

func main() {
    a := []*t{&t{1, "a"}, &t{2, "b"}, &t{3, "c"}, &t{4, "d"}, &t{5, "e"}, &t{6, "f"}}
    k := a[3]
    a = remove(a, 3)
    fmt.Printf("%v  ||  %v", a, k)
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.