I've found that different packages in their installation instructions use either push or add-to-list (For example adding a directory to load-path) and I was wondering what the difference is and what the use case for each would be.
What #zck mentions is one difference. But if that were the only difference then you could ask about
Another important difference:
add-to-list is a function, which means that it evaluates all of its arguments, in particular, the first one.
push is a macro (as is
cl-pushnew) - it does not evaluate its second argument; instead, it interprets it as a generalized place.
For example, if the second argument is a symbol then it is regarded as a variable, and the value of the first argument is consed onto the value of that symbol as a variable, and the variable is set to that new cons.
As the doc string of
This is handy to add some elements to configuration variables, but please do not abuse it in Elisp code, where you are usually better off using `push' or `cl-pushnew'.
From the Emacs documentation, or
C-h f push:
Macro: push element listname
This macro creates a new list whose car is element and whose cdr is the list specified by listname, and saves that list in listname.
From the same page, or
C-h f add-to-list:
Function: add-to-list symbol element &optional append compare-fn
This function sets the variable symbol by consing element onto the old value, if element is not already a member of that value.
add-to-list only pushes if the element isn't already there.
push adds element to the beginning of list.
add-to-list allows you to add element to either the beginning or end of list.
(setq testasdf nil) (push 'a testasdf) testasdf (a) (add-to-list 'testasdf 'b) testasdf (b a) ;; add element to the end (add-to-list 'testasdf "hello" t) testasdf (b a "hello")