append! Vs push! in Julia

Viewed 197

In Julia, you can permanently append elements to an existing vector using append! or push!. For example:

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

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

# or

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

But, what is the difference between append! and push!? According to the official doc it's recommended to:

"Use push! to add individual items to a collection which are not already themselves in another collection. The result of the preceding example is equivalent to push!([1, 2, 3], 4, 5, 6)."

So this is the main difference between these two functions! But, in the example above, I appended individual elements to an existing vector using append!. So why do they recommend using push! in these cases?

2 Answers

append!(v, x) will iterate x, and essentially push! the elements of x to v. push!(v, x) will take x as a whole and add it at the end of v. In your example there is no difference, since in Julia you can iterate a number (it behaves like an iterator with length 1). Here is a better example illustrating the difference:

julia> v = Any[]; # Intentionally using Any just to show the difference

julia> x = [1, 2, 3]; y = [4, 5, 6];

julia> push!(v, x, y);

julia> append!(v, x, y);

julia> v
8-element Vector{Any}:
  [1, 2, 3]
  [4, 5, 6]
 1
 2
 3
 4
 5
 6

In this example, when using push!, x and y becomes elements of v, but when using append! the elements of x and y become elements of v.

Since Julia is still in its early phase it'd be best if you follow the community standards, and one of the community standards, is your code making "sense" to other developers at first sight - I should know what your "intents" are immediately I read your code.

About append!, the doc says:

"For an ordered container collection, add the elements of each collections to the end of it. !!! compat "Julia 1.6" Specifying multiple collections to be appended requires at least Julia 1.6."

The append! method was added and requires Julia 1.6 to use for multiple collections; so in a sense it is the method that's going to be used in the future as Julia gets adopted a lot, Python uses it too, so adopters from there would likely use it too.

About push!, the doc says:

"Insert one or more items in collection. If collection is an ordered container, the items are inserted at the end (in the given order). If collection is ordered, use append! to add all the elements of another collection to it."

The doc advises you use "append!" over "push!" when your collection is ordered. So as a Julia user, if I see append! on your code, I should know the collections its making changes on is in some way "ordered". That's just it. Otherwise, push! and append! does same things(something that might change in the future), but please follow community standards, it will help.

So use append! when you care about order and use push! when order doesn't matter in your collections. This way, any Julia user reading your code, will know your intents right away; but please don't mix them up

Related