# Vector of Vectors with different size to one Vector

**URL:** <https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534>\
**Category:** General Usage\
**Tags:** vector\
**Created:** [June 1, 2020, 4:09am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534 "2020-06-01T04:09:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [June 1, 2020, 4:09am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/1 "2020-06-01T04:09:39Z")

</div>

I am sorry for the easy questions.  
Would you suggest how to convert:  
Vector of Vectors with different size to one Vector

---

<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:** [June 1, 2020, 4:12am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/2 "2020-06-01T04:12:37Z")

</div>

```julia
julia> reduce(vcat, [[1,2,3], [4, 5], [6]])
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

```

For more information, check out `?reduce` and `?vcat`.

---

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [June 1, 2020, 4:15am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/3 "2020-06-01T04:15:16Z")

</div>

Thank you so much

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [June 1, 2020, 5:39am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/4 "2020-06-01T05:39:03Z")

</div>

`vcat` accepts “varargs”, so `reduce` isn’t needed:

```julia
vv = [rand(x) for x in 1:100]
vcat(vv...)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 1, 2020, 5:40am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/5 "2020-06-01T05:40:21Z")

</div>

> [@robsmith11](#):
>
> `vcat` accepts “varargs”, so `reduce` isn’t needed:
> 
> ```julia
> 
> ```

Splatting is a lot less efficient. Use `reduce`

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [June 1, 2020, 5:54am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/6 "2020-06-01T05:54:13Z")

</div>

> [@ChrisRackauckas](#):
>
> Splatting is a lot less efficient. Use `reduce`

Ah didn’t realize that. It is indeed slower for a small number of vectors, but the relative difference decreases as the number grows:

```julia

julia> vv = [rand(x) for x in 1:10];

julia> @btime vcat($vv...);
  152.276 ns (1 allocation: 544 bytes)

julia> @btime reduce(vcat, $vv);
  72.192 ns (1 allocation: 544 bytes)

julia> vv = [rand(x) for x in 1:10^4];

julia> @btime vcat($vv...);
  113.653 ms (3 allocations: 381.58 MiB)

julia> @btime reduce(vcat, $vv);
  113.678 ms (2 allocations: 381.51 MiB)

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 1, 2020, 5:57am UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/7 "2020-06-01T05:57:23Z")

</div>

As an aside, `reduce` may be _slower_ with generators.

```julia
julia> a = (rand(100) for i=1:100);

julia> @btime reduce(hcat,$a);
  689.859 μs (380 allocations: 3.95 MiB)

julia> @btime hcat($a...);
  67.084 μs (210 allocations: 172.09 KiB)

```

Perhaps this is because the type inference doesn’t lead to a concrete type.

```julia
julia> @code_warntype reduce(hcat,a)
Variables
  #self#::Core.Compiler.Const(reduce, false)
  op::Core.Compiler.Const(hcat, false)
  itr::Base.Generator{UnitRange{Int64},var"#26#27"}

Body::Array
1 ─ %1 = Core.NamedTuple()::Core.Compiler.Const(NamedTuple(), false)
│ %2 = Base.pairs(%1)::Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(), false)
│ %3 = Base.:(var"#reduce#195")(%2, #self#, op, itr)::Array
└── return %3

```

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [June 2, 2020, 4:25pm UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/8 "2020-06-02T16:25:00Z")

</div>

A comprehension also works.

```
 julia> let vofv = [[1,2,3], [4,5], [6]]
          [y for v in vofv for y in v]
        end
 6-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6

```

---

<div class="post-metadata">

**Author:** ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)\
**Post date:** [June 2, 2020, 6:17pm UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/10 "2020-06-02T18:17:30Z")

</div>

Yes, but `collect`ing `a` and reducing it is even faster.

```julia
julia> f(x) = reduce(hcat, collect(x))
f (generic function with 1 method)

julia> @btime f($a);
  31.211 μs (103 allocations: 166.58 KiB)

julia> @btime hcat($a...);
  49.138 μs (210 allocations: 172.09 KiB)

```

---

<div class="post-metadata">

**Author:** ![Duane\_Wilson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duane_wilson/32/9506_2.png) [@Duane\_Wilson](https://discourse.julialang.org/u/Duane_Wilson)\
**Post date:** [June 2, 2020, 8:26pm UTC](https://discourse.julialang.org/t/vector-of-vectors-with-different-size-to-one-vector/40534/11 "2020-06-02T20:26:59Z")

</div>

The “proper” answer really depends on what you are planning on doing with the result. If the desire is simply to iterate over each element in turn you may want to consider something like `Iterators.flatten`, for example
