holys
holys

Reputation: 14809

How to combine slices into a slice of tuples in Go (implementing python `zip` function)?

Sometimes, it's convenient to combine two lists into a tuple using zip built-in function in Python. How to make this similarly in Go?

For example:

>>> zip ([1,2],[3,4])
[(1,3), (2,4)]

Upvotes: 28

Views: 21011

Answers (5)

ardnew
ardnew

Reputation: 2086

With Go version 1.23 and iter from the standard library, we can now range over function types to zip two lists (of any two types) very easily:

import "iter"
func Zip[T, U any](t []T, u []U) iter.Seq2[T, U] {
    return func(yield func(T, U) bool) {
        for i := range min(len(t), len(u)) { // range over int (Go 1.22)
            if !yield(t[i], u[i]) {
                return
            }
        }
    }
}

In this case, you don't even need to define a utility type like Pair[T, U], since pairwise sequences are supported natively by this feature.

Consuming the following pair of slices by common index:

var idx = []int{2, 4, 6, 8, 10}
var str = []string{"hello", "my", "darling"}

can be done efficiently one pair at a time:

// Process one at a time
for i, s := range Zip(idx, str) {
    fmt.Printf("%3d: %s\n", i, s)
}

or — with help from Go standard "maps" and "slices" — you can collect all elements at once:

// Collect entire sequence at once
all := maps.Collect(Zip(idx, str))

for _, k := range slices.Sorted(maps.Keys(all)) {
    fmt.Printf("%3d: %s\n", k, all[k])
}

Both of these methods will output the same thing:

2: hello
4: my
6: darling

Try it online

For higher-ordered sets, though, you'd need to use the generalized form iter.Seq[T] with your own T, which could of course be any aggregate/composite type of arbitrary size. There is an analogous Collect function in package slices for use with iter.Seq[T].

Upvotes: 0

Uri Goren
Uri Goren

Reputation: 13700

If you need the result of the zip function to be a map, this can be done with the comparable constraint

func zip[K comparable, V any](a []K, b []V) map[K]V {
    c := make(map[K]V)
    for i := 0; i < len(a); i++ {
        c[a[i]] = b[i]
    }
    return c
}

Upvotes: -2

blackgreen
blackgreen

Reputation: 45071

Go 1.18

With the support for type parameters, you can write a zip function that zips any two slices.

You can declare a tuple struct that can hold any two types, like this:

type Pair[T, U any] struct {
    First  T
    Second U
}

And the zip function. It can be as simple as:

func Zip[T, U any](ts []T, us []U) []Pair[T,U] {
    if len(ts) != len(us) {
        panic("slices have different length")
    }
    pairs := make([]Pair[T,U], len(ts))
    for i := 0; i < len(ts); i++ {
        pairs[i] = Pair[T,U]{ts[i], us[i]}
    }
    return pairs
}

Example usage:

func main() {
    ts := []uint64{100, 200, 300}
    us := []string{"aa", "bb", "cc"}

    p := Zip(ts, us)
    fmt.Println(p)     
    // prints [{100 aa} {200 bb} {300 cc}]
}

You can also modify the function above to zip slices of different lengths, by leaving the Pair field to its zero value for the shorter slice:

func ZipDiff[T, U any](ts []T, us []U) []Pair[T, U] {
    // identify the minimum and maximum lengths
    lmin, lmax := minmax(len(ts), len(us))

    pairs := make([]Pair[T, U], lmax)
    // build tuples up to the minimum length
    for i := 0; i < lmin; i++ {
        pairs[i] = Pair[T, U]{ts[i], us[i]}
    }
    if lmin == lmax {
        return pairs
    }

    // build tuples with one zero value for [lmin,lmax) range
    for i := lmin; i < lmax; i++ {
        p := Pair[T, U]{}
        if len(ts) == lmax {
            p.First = ts[i]
        } else {
            p.Second = us[i]
        }
        pairs[i] = p
    }
    return pairs
}

Example:

func main() {
    ts := []uint64{100}
    us := []string{"aa", "bb", "cc", "dd", "ee"}

    p := ZipDiff(ts, us)
    fmt.Println(p)
    // prints [{100 aa} {0 bb} {0 cc} {0 dd} {0 ee}]

    q := ZipDiff(us, ts)
    fmt.Println(q)
    // prints [{aa 100} {bb 0} {cc 0} {dd 0} {ee 0}]
}

Code and minmax helper func available in the playground: https://go.dev/play/p/jpChqsl_GNl

Upvotes: 5

peterSO
peterSO

Reputation: 166815

To zip some number of slice []int lists,

package main

import "fmt"

func zip(lists ...[]int) func() []int {
    zip := make([]int, len(lists))
    i := 0
    return func() []int {
        for j := range lists {
            if i >= len(lists[j]) {
                return nil
            }
            zip[j] = lists[j][i]
        }
        i++
        return zip
    }
}

func main() {
    a := []int{1, 2, 3}
    b := []int{4, 5, 6}
    c := []int{7, 8, 9, 0}
    iter := zip(a, b, c)
    for tuple := iter(); tuple != nil; tuple = iter() {
        fmt.Println("tuple:", tuple)
    }
}

Output:

tuple: [1 4 7]
tuple: [2 5 8]
tuple: [3 6 9]

Upvotes: 14

thwd
thwd

Reputation: 24848

You could do something like this, where you give the tuple type a name:

package main

import "fmt"

type intTuple struct {
    a, b int
}

func zip(a, b []int) ([]intTuple, error) {

    if len(a) != len(b) {
        return nil, fmt.Errorf("zip: arguments must be of same length")
    }

    r := make([]intTuple, len(a), len(a))

    for i, e := range a {
        r[i] = intTuple{e, b[i]}
    }

    return r, nil
}

func main() {
    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
    b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
    fmt.Println(zip(a, b))
}

Or alternatively use an unnamed type for the tuple, like this:

package main

import "fmt"

func zip(a, b []int) ([][3]int, error) {

    if len(a) != len(b) {
        return nil, fmt.Errorf("zip: arguments must be of same length")
    }

    r := make([][4]int, len(a), len(a))

    for i, e := range a {
        r[i] = [2]int{e, b[i]}
    }

    return r, nil
}

func main() {
    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
    b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
    fmt.Println(zip(a, b))
}

And finally here's a soft-generic way of doing it:

package main

import (
    "fmt"
    "reflect"
)

func zip(a, b, c interface{}) error {

    ta, tb, tc := reflect.TypeOf(a), reflect.TypeOf(b), reflect.TypeOf(c)

    if ta.Kind() != reflect.Slice || tb.Kind() != reflect.Slice || ta != tb {
        return fmt.Errorf("zip: first two arguments must be slices of the same type")
    }

    if tc.Kind() != reflect.Ptr {
        return fmt.Errorf("zip: third argument must be pointer to slice")
    }

    for tc.Kind() == reflect.Ptr {
        tc = tc.Elem()
    }

    if tc.Kind() != reflect.Slice {
        return fmt.Errorf("zip: third argument must be pointer to slice")
    }

    eta, _, etc := ta.Elem(), tb.Elem(), tc.Elem()

    if etc.Kind() != reflect.Array || etc.Len() != 2 {
        return fmt.Errorf("zip: third argument's elements must be an array of length 2")
    }

    if etc.Elem() != eta {
        return fmt.Errorf("zip: third argument's elements must be an array of elements of the same type that the first two arguments are slices of")
    }

    va, vb, vc := reflect.ValueOf(a), reflect.ValueOf(b), reflect.ValueOf(c)

    for vc.Kind() == reflect.Ptr {
        vc = vc.Elem()
    }

    if va.Len() != vb.Len() {
        return fmt.Errorf("zip: first two arguments must have same length")
    }

    for i := 0; i < va.Len(); i++ {
        ea, eb := va.Index(i), vb.Index(i)
        tt := reflect.New(etc).Elem()
        tt.Index(0).Set(ea)
        tt.Index(1).Set(eb)
        vc.Set(reflect.Append(vc, tt))
    }

    return nil
}

func main() {

    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
    b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
    c := [][2]int{}

    e := zip(a, b, &c)

    if e != nil {
        fmt.Println(e)
        return
    }

    fmt.Println(c)
}

Upvotes: 19

Related Questions