Skip to main content

butlast, nbutlast

butlast, nbutlast Function

Syntax:

butlast list &optional n → result-list

nbutlast list &optional n → result-list

Arguments and Values:

list—a list, which might be a dotted list but must not be a circular list.

n—a non-negative integer .

result-list—a list.

Description:

butlast returns a copy of list from which the last n conses have been omitted. If n is not supplied, its value is 1. If there are fewer than n conses in list, nil is returned and, in the case of nbutlast, list is not modified.

nbutlast is like butlast, but nbutlast may modify list. It changes the cdr of the cons n+1 from the end of the list to nil.

Examples:

(setq lst ’(1 2 3 4 5 6 7 8 9))(1 2 3 4 5 6 7 8 9) 
(butlast lst)(1 2 3 4 5 6 7 8)
(butlast lst 5)(1 2 3 4)
(butlast lst (+ 5 5)) → NIL
lst → (1 2 3 4 5 6 7 8 9)
(nbutlast lst 3)(1 2 3 4 5 6)
lst → (1 2 3 4 5 6)
(nbutlast lst 99) → NIL
lst → (1 2 3 4 5 6)
(butlast(a b c d))(A B C)
(butlast((a b) (c d)))((A B))
(butlast(a)) → NIL
(butlast nil) → NIL
(setq foo (list ’a ’b ’c ’d))(A B C D)
(nbutlast foo)(A B C)
foo → (A B C)

(nbutlast (list ’a)) → NIL
(nbutlast()) → NIL

Exceptional Situations:

Should signal an error of type type-error if list is not a proper list or a dotted list. Should signal an error of type type-error if n is not a non-negative integer .

Notes:

(butlast list n) (ldiff list (last list n))

Expanded Reference: butlast, nbutlast

Basic usage of butlast

butlast returns a copy of the list with the last element removed. An optional second argument specifies how many elements to remove from the end.

(butlast '(1 2 3 4 5))
=> (1 2 3 4)

(butlast '(1 2 3 4 5) 2)
=> (1 2 3)

butlast does not modify the original list

butlast always returns a fresh copy, leaving the original list unchanged.

(let ((lst '(a b c d)))
(butlast lst)
lst)
=> (A B C D)

Removing more elements than exist

When n is greater than or equal to the length of the list, butlast returns NIL.

(butlast '(a b c) 3)
=> NIL

(butlast '(a b c) 10)
=> NIL

(butlast '() 1)
=> NIL

nbutlast is the destructive version

nbutlast may modify the original list structure. Always use the return value.

(let ((lst (list 1 2 3 4 5)))
(nbutlast lst 2))
=> (1 2 3)

(let ((lst (list 'a 'b 'c)))
(nbutlast lst)
lst)
=> (A B)

Practical use: removing trailing elements

butlast is useful for processing all but the last n items of a list.

;; Join all path components except the filename
(let ((path-parts '("home" "user" "docs" "file.txt")))
(butlast path-parts))
=> ("home" "user" "docs")