10.4 Association Lists

An association list is a list representing a mapping from one set of values to another; any list whose elements are cons cells is an association list.

Function: cl-assoc item a-list &key :test :test-not :key

This function searches the association list a-list for an element whose CAR matches (in the sense of :test, :test-not, and :key, or by comparison with eql) a given item. It returns the matching element, if any, otherwise nil. It ignores elements of a-list that are not cons cells. (This corresponds to the behavior of assq and assoc in Emacs Lisp; Common Lisp’s assoc ignores nils but considers any other non-cons elements of a-list to be an error.)

Function: cl-rassoc item a-list &key :test :test-not :key

This function searches for an element whose CDR matches item. If a-list represents a mapping, this applies the inverse of the mapping to item.

The cl-assoc-if, cl-assoc-if-not, cl-rassoc-if, and cl-rassoc-if-not functions are defined similarly.

Two simple functions for constructing association lists are:

Function: cl-acons key value alist

This is equivalent to (cons (cons key value) alist).

Function: cl-pairlis keys values &optional alist

This is equivalent to (nconc (cl-mapcar 'cons keys values) alist).