# Inconsistency with \`reduce(vcat, v)\` when the vector has a single element

**URL:** <https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694>\
**Category:** General Usage\
**Tags:** type-stability\
**Created:** [February 3, 2022, 12:35am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694 "2022-02-03T00:35:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [February 3, 2022, 12:35am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694/1 "2022-02-03T00:35:07Z")

</div>

Hello! I guess that this might have been discussed somewhere. But I couldn’t find it. I’ll put an example to show the problem, which in my opinion needs at least some consideration

```julia
julia> vcat(1,2) == reduce(vcat, [1,2])
true

```

On the other hand,

```julia
julia> vcat(1) == reduce(vcat, [1])
false

```

which I find it problematic. The problem comes from `reduce` which behaves differently when a single element is passed as a second argument. Moreover the type is not the same neither if a single element is passed

```julia
julia> reduce(vcat, ["a"])
"a"

julia> reduce(vcat, ["a",["b","c"]])
3-element Vector{String}:
 "a"
 "b"
 "c"

```

I found myself using it together with an extra `vcat` to avoid this corner-case

```julia
julia> vcat(reduce(vcat, ["a"]))
1-element Vector{String}:
 "a"

julia> vcat(reduce(vcat, ["a",["b","c"]]))
3-element Vector{String}:
 "a"
 "b"
 "c"

```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 3, 2022, 2:59am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694/2 "2022-02-03T02:59:20Z")

</div>

[https://github.com/JuliaLang/julia/issues/34395](https://github.com/JuliaLang/julia/issues/34395)

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 3, 2022, 7:10am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694/3 "2022-02-03T07:10:35Z")

</div>

I haven’t compared the performance but I’ve been using  
[https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.flatten](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.flatten)  
to do what I think you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 3, 2022, 8:53am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694/4 "2022-02-03T08:53:42Z")

</div>

See also a [previous discussion](https://discourse.julialang.org/t/why-is-reduce-with-concatenation-type-unstable/45023/2) and the linked issues:

[https://github.com/JuliaLang/julia/issues/31169](https://github.com/JuliaLang/julia/issues/31169)

[https://github.com/JuliaLang/julia/issues/34380](https://github.com/JuliaLang/julia/issues/34380)

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [February 3, 2022, 10:21am UTC](https://discourse.julialang.org/t/inconsistency-with-reduce-vcat-v-when-the-vector-has-a-single-element/75694/5 "2022-02-03T10:21:14Z")

</div>

The problem with flatten is that I obtain characters instead of substrings. Here the example,

```julia
julia> collect(Iterators.flatten(["ab", ["cd"]]))
3-element Vector{Any}:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 "cd"

```

But can be an option if I wrap all the free elements in the array with `["ab"]`

```julia
julia> collect(Iterators.flatten([["ab"], ["cd", "ef"]]))
3-element Vector{String}:
 "ab"
 "cd"
 "ef"

```

UPDATE: It seems to work and since the type-instability was indeed into the array itself, I think this can be a solution for this case. Thanks @lawless-m and thank you all for your help. Interesting to find these issues you mentioned.
