# Trouble with \`append!\`

**URL:** <https://discourse.julialang.org/t/trouble-with-append/109706>\
**Category:** General Usage\
**Created:** [February 4, 2024, 6:55pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706 "2024-02-04T18:55:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [February 4, 2024, 6:55pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/1 "2024-02-04T18:55:04Z")

</div>

I often want to add an item to the end of a list (i.e., to a `Vector`). This works fine:

```julia
julia> x = [1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> append!(x,5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

But this does not:

```julia
julia> x = ["do", "re", "mi", "fa"]
4-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"

julia> append!(x,"sol")
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String

```

I realize I could do this instead:

```julia
julia> x = vcat(x, "sol")
5-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"
 "sol"

```

but the behavior of `append!` for lists of strings seems like a bug.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2024, 6:57pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/2 "2024-02-04T18:57:14Z")

</div>

`append!` is for appending one collection to another, you should be using `push!` for both your use cases.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 4, 2024, 7:04pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/3 "2024-02-04T19:04:27Z")

</div>

And the reason why `append!` works in the first case is that numbers are iterable. As it happens so are strings, but they iterate over their characters, which causes the type mismatch in the second case.

---

<div class="post-metadata">

**Author:** ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)\
**Post date:** [February 4, 2024, 7:09pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/4 "2024-02-04T19:09:00Z")

</div>

I think what’s particularly confusing here is that single numbers are “iterable”, yielding a single element.

For example

```julia
x = 42
for i in x
    println(i)
end

```

will print 42.

Looking at the method hit for `append!(x, 5)`, we see

```julia
julia> methods(append!, Tuple{Vector{Int}, Int})
# 1 method for generic function "append!" from Base:
 [1] append!(a::AbstractVector, iter)
     @ array.jl:1187

```

Same if we checked for args of `Tuple{Vector{String}, String}`.

So when using `append!`, julia will try to iterate through the second argument, adding it’s elements to the first argument. The confusing part happens when iterating through a number yields a single number of the same type, while iterating though a string yields a sequence of `Char`, which is of a different type from the element type of the first argument.

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [February 6, 2024, 7:30pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/5 "2024-02-06T19:30:37Z")

</div>

Thanks. That makes sense. Or I can do this:

```julia
julia> x = ["do", "re", "mi", "fa"]
4-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"

julia> append!(x, ["sol"])
5-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"
 "sol"

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 6, 2024, 7:32pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/6 "2024-02-06T19:32:42Z")

</div>

Yes. But you should probably do the following if you know that you only have one element.

```julia-repl
julia> x = ["do", "re", "mi", "fa"]
4-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"

julia> push!(x, "sol")
5-element Vector{String}:
 "do"
 "re"
 "mi"
 "fa"
 "sol"

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 6, 2024, 7:57pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/7 "2024-02-06T19:57:09Z")

</div>

> [@scheinerman](#):
>
> `append!(x, ["sol"])`

This works, but creates an unnecessary temporary array, which takes extra time and memory.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [February 6, 2024, 11:53pm UTC](https://discourse.julialang.org/t/trouble-with-append/109706/8 "2024-02-06T23:53:26Z")

</div>

If one insisted on using `append!` (for whatever reason), one could do

```julia
append!(x, ("sol", ))

```

to avoid the superfluous allocation.
