Elisp: Association List

By Xah Lee. Date: . Last updated: .

What is Association List

Association List (aka alist) is a List , where each element is a Cons Pair , like this

(cons key value)

Difference Between Association List and Hash Table

Difference Between List of Pairs and Association List

Dot Notation for Cons

when creating a association list, For example

(list (cons k1 v1) (cons k2 v2) )

if you do not need to evaluate the items, you can write it as

'( (k1 . v1) (k2 . v2) )

〔see Elisp: Quote and Dot Notation

Create a Alist

String key example

;; example of association list
(setq
 xx
 (list
  (cons "aa" 23)
  (cons "bb" 24)
  (cons "cc" 33)))

;; dot notation
(setq
 xx
 '(("aa" . 23)
   ("bb" . 24)
   ("cc" . 33)))

Symbol key example

Elisp: Symbol

;; example of association list
;; where keys are symbols, values are numbers

(setq
 xx
 (list
  (cons 'aa 23)
  (cons 'bb 24)
  (cons 'cc 33)))

;; dot notation
(setq
 xx
 '((aa . 23)
   (bb . 24)
   (cc . 33)))

Symbol Key vs String Key

Some functions for alist use eq to test existence of key, and others use equal. Some lets you specify a function for equality test.

So, it is important to know:

💡 TIP: I recommend using string as keys whenever possible. Because that is simpler. Using symbol key pollutes the symbol table obarray. Use symbol key only when you need to work with lisp function names or variable names.

〔see Elisp: Equality Test

Get Value by Key

alist-get
(alist-get key alist &optional default remove testfn)

Find the first key, return its value. If no exist, return default. Default to nil.

The remove should be nil. (it is used in advanced situation with Place Expression . Best to avoid. )

key existence is checked by testfn (must be a symbol of function). Default to eq. 〔see Elisp: Equality Test

(setq xx
      '(("aa" . 23)
        ("bb" . 24)
        ("cc" . 33)))

;; get the value with key "bb". if not found, return 999. use string-equal for comparison
(alist-get "bb" xx 999 nil 'string-equal)
;; 24

(alist-get "dd" xx 999 nil 'string-equal)
;; 999
(setq xx
      '((aa . 23)
        (bb . 24)
        (cc . 33)))

;; get the value with symbol key bb. if not found, return 999
(alist-get 'bb xx 999)
;; 24

(alist-get 'dd xx 999)
;; 999

Get Cons Cell by Key

assoc-string

(assoc-string KEY LIST &optional CASE-FOLD)

Designed specifically for association list of string keys.

  • Return the first cons cell (or atom) with the key.
  • If no exist, return nil.
  • Element in the list can be just a string or symbol, instead of a Cons Cell.
  • Works also if keys are symbol type.
  • When doing the comparison, symbols are first converted to strings, and unibyte strings to multibyte.
  • CASE-FOLD is t or nil. If t, both KEY and the elements of LIST are upcased for comparison.
(setq xx
      (list
       (cons "aa" 23)
       (cons "bb" 24)
       (cons "cc" 33)))

;; get the cons cell with key "bb"
(assoc-string "bb" xx)
;; ("bb" . 24)
;; demo assoc-string, showing the list element needs not be a cons cell
(setq xx
      (list
       (cons "aa" 23)
       "bb"
       (cons "cc" 33)))

;; get the cons cell with key "bb"
(assoc-string "bb" xx)
;; "bb"
assoc
(assoc key alist)
  • Return the first cons cell with the key.
  • If no exist, return nil.
  • Key existence is checked by equal. 〔see Elisp: Equality Test

💡 TIP: use this if all keys are symbols, or strings, or integers.

(setq xx
      (list
       (cons "aa" 23)
       (cons "bb" 24)
       (cons "cc" 33)))

;; get the cons cell with key "bb"
(assoc "bb" xx)
;; ("bb" . 24)
assq
(assq key alist)

similar to assoc except key is checked by eq.

〔see Elisp: Equality Test

💡 TIP: use this if all keys are symbols, or integers.

(setq xx
      '((aa . 23)
        (bb . 24)
        (cc . 33)))

(assq 'aa xx)

Check If a Key Exist

Use assoc or assq.

Get Cons Cell by Value

You can search alist by value.

rassoc
(rassoc value alist)
  • Return the first cons cell that has specific value.
  • Else return nil.
  • Test is done by equal. 〔see Elisp: Equality Test
(setq xx
      '(("aa" . 23)
        ("bb" . 24)
        ("cc" . 33)))

;; get the cons cell with value 24
(rassoc 24 xx)
; return ("bb" . 24)
rassq
(rassq value alist)

similar to rassoc except value is compared using eq. 〔see Elisp: Equality Test

Add Item

Add a Item to Front

Add a element to the front. Use push

(setq xx
      '(("aa" . 23)
        ("bb" . 24)
        ))

;; add a element
(push '("cc" . 33) xx)

Add Item If Key No Exist

(setq xx
      '(("aa" . 23)
        ("bb" . 24)
        ))

(when (not (assoc "cc" xx))
  (push '("cc" . 33) xx))

Delete Items by Key

assoc-delete-all
(assoc-delete-all KEY ALIST &optional TEST)
  • Delete all elements that has key KEY.
  • Keys are checked by TEST. Defaults to equal.
  • Return the modified alist.
  • Elements of ALIST that are not cons are ignored.
(setq xx
      '(("aa" . 23)
        ("bb" . 24)
        ))

(setq xx (assoc-delete-all "bb" xx))
assq-delete-all

similar to assoc-delete-all but uses eq for testing key.

Delete Item by Value

rassq-delete-all

(rassq-delete-all VALUE ALIST)

  • Delete all elements whose value matches.
  • values are checked by eq.
  • Return the modified alist.
  • Elements of ALIST that are not cons are ignored.
(setq xx
      (list
       (cons "aa" 3)
       (cons "bb" 4)
       (cons "cc" 4)
       (cons "dd" 5)))

(rassq-delete-all 4 xx)
;; (("aa" . 3) ("dd" . 5))

More Alist Functions

here are more specialized function for alist.

assoc-default

(assoc-default KEY ALIST &optional TEST DEFAULT)

Designed for list whose element can be either Cons Cell or not. And valid match is considered only if element is cons cell. Return the cdr part of match.

  • If a match is found, return cdr of cons if the item is cons, else return DEFAULT.
  • If no match, return nil.
  • A match is determined by the function TEST.
  • TEST is given two arguments item and KEY, in that order.
  • item is either the car of cons, or the element itself if it's not a cons cell.
  • by default, TEST is equal.
;; demo assoc-default

(setq xx
      (list
       (cons "aa" 2)
       "bb"
       (cons "bb" 3)
       (cons "cc" 4)
       (list "dd" 5)))

;; match found, return its cdr
(assoc-default "aa" xx )
;; 2

;; match found, return its cdr
(assoc-default "dd" xx )
;; (5)

;; match found, but bb is not a cons, so return nil
(assoc-default "bb" xx )
;; nil

;; no match
(assoc-default "ee" xx )
;; nil

Association Lists (ELISP Manual)

Emacs Lisp, list

Special Lists

List Structure

Emacs Lisp, key value collection