# All vectors formed by picking one element from each vector

**URL:** https://discourse.julialang.org/t/all-vectors-formed-by-picking-one-element-from-each-vector/49950
**Category:** General Usage
**Created:** [November 10, 2020, 10:23pm UTC](https://discourse.julialang.org/t/all-vectors-formed-by-picking-one-element-from-each-vector/49950 "2020-11-10T22:23:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [November 10, 2020, 10:23pm UTC](https://discourse.julialang.org/t/all-vectors-formed-by-picking-one-element-from-each-vector/49950/1 "2020-11-10T22:23:56Z")

</div>

I’d like to write a vararg function which takes N vectors, and return all possible length-N vectors by picking one element from each vector. For example

```
all_vectors([['a', 'b'], [1, 2, 3], ['x']])

```

should give the result

```
[['a', 1, 'x'], ['a', 2, 'x'], ['a', 3, 'x'], ['b', 1, 'x'], ['b', 2, 'x'], ['b', 3, 'x']]

```

Although it’s not hard to write such a function, I’m wondering what the most concise and readable solution would look like.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 10, 2020, 10:29pm UTC](https://discourse.julialang.org/t/all-vectors-formed-by-picking-one-element-from-each-vector/49950/2 "2020-11-10T22:29:26Z")

</div>

Check out the built-in `Iterators.product` function:

```julia
help?> Iterators.product
  product(iters...)

  Return an iterator over the product of several iterators. Each generated element is a tuple whose ith element comes from the ith argument iterator. The first iterator changes the fastest.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> collect(Iterators.product(1:2, 3:5))
  2×3 Array{Tuple{Int64,Int64},2}:
   (1, 3) (1, 4) (1, 5)
   (2, 3) (2, 4) (2, 5)

```

Note that `product` expects each iterator to be a separate argument, so you can take the product of an array of iterators by doing `product(my_vector_of_vectors...)` using `...` to splat the collection into multiple arguments.

Also, you may get better performance by storing a _tuple_ of vectors instead of a vector of vectors, since your individual inner vectors have different types. That would look like:

```julia
julia> data = (['a', 'b'], [1, 2, 3], ['x'])
(['a', 'b'], [1, 2, 3], ['x'])

julia> Iterators.product(data...)

```

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [November 10, 2020, 10:34pm UTC](https://discourse.julialang.org/t/all-vectors-formed-by-picking-one-element-from-each-vector/49950/3 "2020-11-10T22:34:47Z")

</div>

Wow, that’s a very quick solution, thanks!
