listgoslice

What's the Go idiom for Python's list.pop() method?


In Python, I have the following:

i = series.index(s) # standard Python list.index() function
tmp = series.pop(i)
blah = f(tmp)
series.append(tmp)

In converting this to Go, I am looking for a similar way of retrieving an item from a slice by index, doing something with it, then putting the original item at the end of my slice.

From here, I have arrived at the following:

i = Index(series, s) // my custom index function...
tmp, series = series[i], series[i+1:]
blah := f(tmp)
series = append(series, tmp)

But this fails at the end of lists:

panic: runtime error: slice bounds out of range

How would I idiomatically translate this slice.pop() into Go?


Solution

  • The "Cut" trick in the linked document does what you want:

    xs := []int{1, 2, 3, 4, 5}
    
    i := 0 // Any valid index, however you happen to get it.
    x := xs[i]
    xs = append(xs[:i], xs[i+1:]...)
    // Now "x" is the ith element and "xs" has the ith element removed.
    

    Note that if you try to make a one-liner out of the get-and-cut operations you'll get unexpected results due to the tricky behavior of multiple assignments in which functions are called before other expressions are evaluated:

    i := 0
    x, xs := xs[i], append(xs[:i], xs[i+1:]...)
    // XXX: x=2, xs=[]int{2, 3, 4, 5}
    

    You can work around by wrapping the element access operation in any function call, such as the identity function:

    i := 0
    id := func(z int) { return z }
    x, xs := id(xs[i]), append(xs[:i], xs[i+1:]...)
    // OK: x=1, xs=[]int{2, 3, 4, 5}
    

    However, at that point it's probably more clear to use separate assignments.

    For completeness, a "cut" function and its usage could look like this:

    func cut(i int, xs []int) (int, []int) {
      y := xs[i]
      ys := append(xs[:i], xs[i+1:]...)
      return y, ys
    }
    
    t, series := cut(i, series)
    f(t)
    series = append(series, t)