User Tools



  • union list-1 list-2 &key key test test-notresult-list
  • nunion list-1 list-2 &key key test test-notresult-list

Arguments and Values


union and nunion return a list that contains every element that occurs in either list-1 or list-2.

For all possible ordered pairs consisting of one element from list-1 and one element from list-2, :test or :test-not is used to determine whether they satisfy the test. The first argument to the :test or :test-not function is the part of the element of list-1 extracted by the :key function (if supplied); the second argument is the part of the element of list-2 extracted by the :key function (if supplied).

The argument to the :key function is an element of list-1 or list-2; the return value is part of the supplied element. If :key is not supplied or nil, the element of list-1 or list-2 itself is supplied to the :test or :test-not function.

For every matching pair, one of the two elements of the pair will be in the result. Any element from either list-1 or list-2 that matches no element of the other will appear in the result.

If there is a duplication between list-1 and list-2, only one of the duplicate instances will be in the result. If either list-1 or list-2 has duplicate entries within it, the redundant entries might or might not appear in the result.

The order of elements in the result do not have to reflect the ordering of list-1 or list-2 in any way. The result list may be eq to either list-1 or list-2 if appropriate.


(union '(a b c) '(f a d))

(A B C F D) or (B C F A D) or (D F A B C)

(union '((x 5) (y 6)) '((z 2) (x 4)) :key #'car)

((X 5) (Y 6) (Z 2)) or ((X 4) (Y 6) (Z 2))

(defparameter *list-1* (list 1 2 '(1 2) "a" "b"))


(defparameter *list-2* (list 2 3 '(2 3) "B" "C"))


(nunion *list-1* *list-2*)

(1 (1 2) "a" "b" 2 3 (2 3) "B" "C") or (1 2 (1 2) "a" "b" "C" "B" (2 3) 3)

Side Effects

nunion is permitted to modify any part, car or cdr, of the list structure of list-1 or list-2.

Affected By


Exceptional Situations

Should be prepared to signal an error of type type-error if list-1 and list-2 are not proper lists.

See Also

Function INTERSECTION, {\secref\ConstantModification}, {\secref\TraversalRules}

Example Implementation

To be done.


The :test-not parameter is deprecated.

Since the nunion side effect is not required it should not be used in for-effect-only positions in portable code.