Elisp: Difference Between Association List vs List of Pairs
In an
Association List
, each element is a Cons Cell,
e.g.
(cons 3 4)
In a list of pairs, each element is a list of 2 elements.
e.g.
(list 3 4)
(setq x-alist (list (cons "mary" 23) (cons "john" 24) (cons "smith" 33))) (setq x-pairs (list (list "mary" 23) (list "john" 24) (list "smith" 33))) ;; get first element (nth 0 x-alist) ;; ("mary" . 23) ;; result is a cons cell ;; get first element (nth 0 x-pairs) ;; ("mary" 23) ;; result is a proper list, of 2 elements
Which One Should You Use
If you want a list of pairs, best to use Association List or Hash Table . When you use alist, there are many builtin functions that work on the keys or values.
Use list of list if the pair is not used as “key and value”, or if might extend the pair into more than 2 items.