# Append!() with Tuple and Dict

**URL:** <https://discourse.julialang.org/t/append-with-tuple-and-dict/6471>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 16, 2017, 1:58pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471 "2017-10-16T13:58:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![giuan](https://avatars.discourse-cdn.com/v4/letter/g/ecb155/32.png) [@giuan](https://discourse.julialang.org/u/giuan)\
**Post date:** [October 16, 2017, 1:58pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/1 "2017-10-16T13:58:55Z")

</div>

Why

```julia
x = Any[1,2]
append!(x,Dict("a"=>1,"b"=>2))
append!(x,("j","k"))
println(x)

```

print `Any[1, 2, "b"=>2, "a"=>1, "j", "k"]`?

In python

```julia
x = [1,2]
x.append({"a":1,"b":2})
x.append(("a","b"))
print(x)

```

print `[1, 2, {'a': 1, 'b': 2}, ('a', 'b')]`

Also `Base.Iterators.flatten`  
`[e for e in Base.Iterators.flatten([1,2,Dict("a"=>1,"b"=>2),("j","k")])]`  
 → `Any[1, 2, "b"=>2, "a"=>1, "j", "k"]`

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 16, 2017, 2:11pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/2 "2017-10-16T14:11:06Z")

</div>

Python’s append is `push!`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 16, 2017, 2:12pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/3 "2017-10-16T14:12:00Z")

</div>

```julia
x = Any[1, 2]
push!(x, Dict("a"=>1, "b"=>2))
push!(x, ("j", "k"))

println(x)
Any[1, 2, Dict("b"=>2,"a"=>1), ("j", "k")]

```

---

<div class="post-metadata">

**Author:** ![giuan](https://avatars.discourse-cdn.com/v4/letter/g/ecb155/32.png) [@giuan](https://discourse.julialang.org/u/giuan)\
**Post date:** [October 16, 2017, 2:40pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/4 "2017-10-16T14:40:22Z")

</div>

Sorry, python was a bad example!  
I know push!  
My question is: why append! does’t works like vcat?

I expected the same result for

```julia
x = Any[]
append!(x,[1,2])
append!(x,Dict("a"=>1,"b"=>2))
append!(x,("j","k"))
println(x)

```

result `Any[1, 2, "b"=>2, "a"=>1, "j", "k"]`

and

```julia
x = Any[]
x = vcat(x,[1,2])
x = vcat(x,Dict("a"=>1,"b"=>2))
x = vcat(x,("j","k"))
println(x)

```

result `Any[1, 2, Dict("b"=>2,"a"=>1), ("j", "k")]`  
Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 16, 2017, 3:14pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/5 "2017-10-16T15:14:37Z")

</div>

`append!` is a generic operation on container. The argument you add is **ALWAYS** treated as an iterator and all it’s content is added to the array. There’s no logic for adding an object as element and that’s why there’s `push!` when you need to treat arbitrary objects as a whole.

`vcat` is a function for arrays and you should not expect it to work the same way on non-arrays as on arrays since it concatenate in the v(ertical) direction which is only a valid concept for arrays. Therefore, it treats arrays and non-arrays differently (I feel like this is just repeating myself, oh well…).  
This works very well most of the case and is only mainly a problem when you want to treat an array as a single element, i.e. getting `[[1, 2], [2, 3], [1]]` from `a = [[1, 2], [2, 3]]; b = [1]`. Fortunately, what `vcat` is doing is roughly equivalent to implicitly wrapping non-array with a vector so you can do that implicitly and get it with `[a; [b]]`. This should be roughly equivalent to a non-mutating `push!` for vectors.

---

<div class="post-metadata">

**Author:** ![giuan](https://avatars.discourse-cdn.com/v4/letter/g/ecb155/32.png) [@giuan](https://discourse.julialang.org/u/giuan)\
**Post date:** [October 16, 2017, 3:33pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/6 "2017-10-16T15:33:13Z")

</div>

Thanks!

I’am working with 1-dimensional arrays as lists.  
This function is what i need:

```julia
myappend!(v::Vector, e::Vector) = append!(v,e)
myappend!(v::Vector, e::Any) = push!(v,e)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 16, 2017, 3:59pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/7 "2017-10-16T15:59:09Z")

</div>

In general, it is not a great idea to treat vectors special like this. (see the problem I mentioned above with `vcat` doing this). It’s usually better to just specify explicitly what you need and I’ve rarely (never?) found that to be a problem, it’s usually (always?) very clear at the call site what the caller want. Doing this mainly makes your code not able to handle vector of vector nicely.

---

<div class="post-metadata">

**Author:** ![Rogue\_Nation](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogue_nation/32/10282_2.png) [@Rogue\_Nation](https://discourse.julialang.org/u/Rogue_Nation)\
**Post date:** [September 13, 2019, 3:44pm UTC](https://discourse.julialang.org/t/append-with-tuple-and-dict/6471/8 "2019-09-13T15:44:25Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.
