Difference between push! and append! ?

9,758 views
Skip to first unread message

Randy Zwitch

unread,
Aug 7, 2013, 2:49:09 PM8/7/13
to julia...@googlegroups.com
Reading through documentation, it isn't really clear to me what the difference between push! and append! is. Meaning, coming from a Python background, I would've expected the behavior of push! to be named append!, as in Python:

Python:

In [3]: nums = [1,1]

In [4]: nums.append(2)

In [5]: nums
Out[5]: [1, 1, 2]


Julia

julia> nums = [1,1]
2-element Int64 Array:
 1
 1

julia> append!(nums, 2)
ERROR: no method append!(Array{Int64,1},Int64)

julia> push!(nums, 2)
3-element Int64 Array:
 1
 1
 2

 What am I appending when I use append! and not push!, another object?

Stefan Karpinski

unread,
Aug 7, 2013, 2:59:52 PM8/7/13
to Julia Users
The second argument of push! is a single element to be pushed onto the end to the first argument while the second argument of append! is a collection whose elements are to be pushed onto the end of the first argument. This is another one of those places where Python has insisted on using non-standard (arguably incorrect) terminology such as calling its arrays "lists".

Simon Kornblith

unread,
Aug 7, 2013, 3:00:49 PM8/7/13
to julia...@googlegroups.com

You're correct that push! is equivalent to Python's append. append! is equivalent to Python's extend. (I agree that this is a somewhat unfortunate name for people coming from Python.)

Ivar Nesje

unread,
Aug 7, 2013, 3:03:42 PM8/7/13
to julia...@googlegroups.com
julia> help(append!)
Loading help data...
Base.append!(collection, items) -> collection

   Add the elements of "items" to the end of a collection.

julia> help(push!)
Base.push!(collection, item) -> collection

   Insert an item at the end of a collection.

The difference seems to be append! takes a collection and add all the elements to another collection. push! takes a single argument and insert it at the end of the collection. They need different names because julia does not know if you want to push! the collection at the end of the previous collection or to append all the elements of the collection.

Ivar

Randy Zwitch

unread,
Aug 7, 2013, 3:10:42 PM8/7/13
to julia...@googlegroups.com
Thanks. So it's really as simple as this case:


julia> nums = [1,1]
2-element Int64 Array:
 1
 1

julia> nums2 = [2,2,2]
3-element Int64 Array:
 2
 2
 2

julia> append!(nums, nums2)
5-element Int64 Array:
 1
 1
 2
 2
 2


In terms of terminology, what are you referencing in terms of what the 'correct' terminology is?

John Myles White

unread,
Aug 7, 2013, 3:59:33 PM8/7/13
to julia...@googlegroups.com
push is very traditional CS terminology. I'm not sure about append.

 -- John

Stefan Karpinski

unread,
Aug 7, 2013, 4:02:02 PM8/7/13
to julia...@googlegroups.com
I believe that append had been traditional in lisp and other languages since the 50s.

Ryan Gardner

unread,
Mar 2, 2016, 12:16:28 PM3/2/16
to julia-users
Can anyone comment on the underlying memory allocation behavior of these two operations?  I believe push! will potentially allocate more memory than needed for the resulting array in the case that any new memory needed to be allocated?  Does it double the size of the underlying allocated memory?  Will append! do something similar?

Most importantly, is there an easy way to find this information?  E.g., is there a direct way I can find out how much memory (maybe in units of elements) is allocated under an array?

I need to grow some arrays (conceptually by appending data from one to another), and this is going to happen a lot such that it would be very bad if the first array was copied for every single append.

Thanks.

Lutfullah Tomak

unread,
Mar 2, 2016, 12:39:52 PM3/2/16
to julia-users
Hi Ryan,
I believe you need to look at sizehint if you need to use push! or append! a lot

Here a thread for it https://groups.google.com/forum/m/#!topic/julia-users/214uuJcCF6o
and julia doc
and reference http://docs.julialang.org/en/release-0.4/stdlib/collections/#Base.sizehint!

Best,

Tim Holy

unread,
Mar 2, 2016, 12:49:54 PM3/2/16
to julia...@googlegroups.com
You can find out the answers yourself by looking at the number of allocations
reported by @time. From that, you'll discover that julia doubles the size of
the storage whenever it reallocates (so it uses ~log2(N) allocations).

--Tim
Reply all
Reply to author
Forward
0 new messages